简体   繁体   English

HttpPostedFileBase始终为null

[英]HttpPostedFileBase is always null

I have controller like this : 我有这样的控制器:

public ActionResult SaveBook(Book coll, HttpPostedFileBase EBook)
{
}

and view like the following: 并如下图所示:

@using (Html.BeginForm("SaveBook", "Book", FormMethod.Post,
            new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Book</legend>

        <table>
            <tr>
                <td>@Html.LabelFor(model => model.Title)</td>
                <td>@Html.EditorFor(model => model.Title)@Html.ValidationMessageFor(model => model.Title)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.ISBN)</td>
                <td>@Html.EditorFor(model => model.ISBN)@Html.ValidationMessageFor(model => model.ISBN)</td>
            </tr>
 <tr><td>@Html.LabelFor(model => model.EBook) </td><td>@Html.TextBoxFor(model => model.EBook, new { type = "file", accept = ".pdf" })
    @Html.ValidationMessageFor(model => model.EBook)</td></tr>
        </table>
        <p>
            <input type="submit" value="SaveBook" />
        </p>
    </fieldset>
}

My Model like this: 我的模特是这样的:

[Required(ErrorMessage = "Title Required")]
public  String  Title { get; set; }
[Required(ErrorMessage = "ISBN Required")]
public  String  ISBN { get; set; }
public HttpPostedFileBase EBook { get; set; }

But still I am getting value fro EBook "null" value in "EBook" object or in the "coll" object of the Controller. 但是我仍然从“ EBook”对象或Controller的“ coll”对象中的EBook“ null”值中获取值。 Any suggestion? 有什么建议吗?

Since you have same name in your model, you are getting it null. 由于您的模型名称相同,因此将其设为空。 happened to me yesterday as well rename one of them to something else. 昨天我发生的事以及将其中一个重命名为其他名称。

Try following: 请尝试以下操作:

    <div class="editor-field">
        <input type="file" name="file" />
    </div>

Similarlar you action should be 类似的动作应该是

public ActionResult SaveBook(Book coll, HttpPostedFileBase file)
{
}

EBook is already a property of your Model. 电子书已经是您的模型的属性。 Change your Action to the following for the model binding to work properly: 将“操作”更改为以下内容,以使模型绑定正常工作:

[HttpPost]
public ActionResult SaveBook(Book coll)
{ 
}

The fact I found now. 我现在发现的事实。 I increase maxRequestLength like: 我增加maxRequestLength像:

  <httpRuntime targetFramework="4.5" maxRequestLength="1048576" />

and it worked. 而且有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM