Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的HttpPostedFileBase总是空的?_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 为什么我的HttpPostedFileBase总是空的?

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

这看起来和其他问题类似,但他们遇到的所有问题都是HTML中输入type=“file”的名称和控制器中参数的名称之间的命名不匹配。我已经多次检查和复查了很多东西,但HttpPostedFileBase的值始终返回为null,这导致控制器中出现NullReferenceException。 代码如下:- HTML代码:-

    @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" })