1

我有这样的控制器:

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

并查看如下:

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

我的模型是这样的:

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

但我仍然从“EBook”对象或控制器的“coll”对象中的 EBook“null”值中获得价值。有什么建议吗?

4

3 回答 3

0

The fact I found now. I increase maxRequestLength like:

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

and it worked.

于 2013-09-11T14:44:33.173 回答
0

EBook 已经是您的模型的属性。将您的 Action 更改为以下内容以使模型绑定正常工作:

[HttpPost]
public ActionResult SaveBook(Book coll)
{ 
}
于 2013-09-11T10:22:04.753 回答
0

由于您的模型中有相同的名称,因此您将其设为空。昨天发生在我身上,也将其中一个重命名为其他名称。

尝试以下操作:

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

同样你的行动应该是

public ActionResult SaveBook(Book coll, HttpPostedFileBase file)
{
}
于 2013-09-11T09:59:13.013 回答