C# 为什么我的HttpPostedFileBase总是空的?
这看起来和其他问题类似,但他们遇到的所有问题都是HTML中输入type=“file”的名称和控制器中参数的名称之间的命名不匹配。我已经多次检查和复查了很多东西,但HttpPostedFileBase的值始终返回为null,这导致控制器中出现NullReferenceException。 代码如下:- HTML代码:-C# 为什么我的HttpPostedFileBase总是空的?,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,这看起来和其他问题类似,但他们遇到的所有问题都是HTML中输入type=“file”的名称和控制器中参数的名称之间的命名不匹配。我已经多次检查和复查了很多东西,但HttpPostedFileBase的值始终返回为null,这导致控制器中出现NullReferenceException。 代码如下:- HTML代码:- @using (Html.BeginForm(new { enctype = "multipart/form-data" })) { @Html.Validati
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Please Fill in the following details and Upload your Design Pic</legend>
<div class="editor-label">
@Html.LabelFor(model => model.chest)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.chest)
@Html.ValidationMessageFor(model => model.chest)
</div>
<p>
<label for="file">Design File Pic:</label>
<input type="file" name="file" />
</p>
<p>
<input type="submit" value="Order" />
</p>
如果(file.ContentLength>0)--NullreferenceException,则第行出现错误。因为
Html.BeginForm(object)
用于路由值,而不是元素属性。您需要对属性使用更大的重载。试试这个:
Html.BeginForm("action", "controller", null, FormMethod.Post, new { enctype = "multipart/form-data" })
请看这里,了解我的意思:
Html.BeginForm("action", "controller", null, FormMethod.Post, new { enctype = "multipart/form-data" })