gpt4 book ai didi

c# - MVC HttpPost 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:05 25 4
gpt4 key购买 nike

我在执行这段代码时出现空对象引用异常。我有一个文件 html 控件,我的 Razor 代码如下所示:

@using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post))
{
<p><label for="filename">Filename : </label><input type="text" name="filename" /></p>
<p><label for="description">Description : </label><input type="text" name="description" /></p>
<p><input type="file" name="file" id="file" /> </p>
<p><input type="hidden" value="@Model[2]" name="catid"/></p>

<input type="submit" value="Upload Now!" />
<input type="reset" value="Reset" />
}

我的 AdministerFilesController 是这样的:

[HttpPost]
public ActionResult AddFiles(HttpPostedFileBase file, int catid, string description)
{
// Verify that the user selected a file {
if (file != null && file.ContentLength > 0)
{
// extract only the filename
var filename = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/Uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), filename);
file.SaveAs(path);
}
RefDataLinks_mst fileDetails = new RefDataLinks_mst()
{
LastModified = new DateTime(2012,1,1),
CategoryID = catid,
DataFileName = Path.GetFileName(file.FileName),
DataTitle = "SAMPLETITLE",
DataID = ((new Random()).Next(100, 1000)),
DataFilePath = "Sample/Path",
Description = description,
UpdatedBy = "Arjel",
FileSize = file.ContentLength
};
bool b = ServiceReferenceHelper.AddFile(fileDetails);
// Redirect back to the index action to show the form once again
return Redirect("AddFiles?catid=" + catid); //both works
//return RedirectToAction("ViewDataFiles", new { catid = catid });
}

收到来自 catid 和描述的数据,但文件引用了一个空对象。可能是什么问题?

最佳答案

上传实际上需要您在表单上指定 enctype 属性。以下是如何执行此操作的示例:

@using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post, 
new { enctype = "multipart/form-data" }))
{
...
}

关于c# - MVC HttpPost 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com