2

我曾经有一个用于上传文件的表单,它工作正常,但令我惊讶的是,它不再将 HttpPostedFileBase 绑定到模型。过了一会儿,我意识到这是因为我已经从

@using (Html.BeginForm("AddDocument", "Documents", FormMethod.Post, new { enctype = "multipart/form-data", @class = "addTab" }))

@using (Html.BeginForm("AddDocument", "Documents", FormMethod.Post, new { @id = "addForm", enctype = "multipart/form-data", @class = "addTab" }))

所以我改为(我尝试只删除id的@,但它也没有工作)

@using (Html.BeginForm("AddDocument", "Documents", new {id= "addForm"}, FormMethod.Post, new { enctype = "multipart/form-data", @class = "addTab" }))

它再次起作用。

所以我认为我做了很多错误的事情,我已经改变了我的解决方案中的其他形式。但事实证明,如果我使用

@using (Html.BeginForm("Edit", "Events", new {id = "editForm"}, FormMethod.Post, new { @class = "addTab" }))

代替

@using (Html.BeginForm("AddContact", "Contacts", FormMethod.Post, new { @id = "addForm", @class = "addTab" }))

我无法设置 ID(也无法使用其他选项)

我正在尝试通过msdn 文档下定决心,但我什至不知道 id 是否应该被视为路由值或 html 属性,@ 的用途,有时使用有时不使用,特别是为什么我丢失了多部分情况下的绑定

更新:

这是视图(我尝试使用具有相同结果的注释掉的代码):

@model AddDocumentViewModel

@using (Html.BeginForm("AddDocument", "Documents", new {id= "addForm"}, FormMethod.Post, new { enctype = "multipart/form-data", @class = "addTab" }))
{   
    @Html.ValidationSummary(true, "Please correct the errors and try again.")
    <div id="addDocumentdetails">
        <div>

            @*<input id="File" type="file" name="File" data-val-required="Please select a file" data-val="true" />*@
            @*@Html.ValidationMessage("File")*@
            @Html.TextBoxFor(m => m.File, new { type = "file" })
            @Html.ValidationMessageFor(m => m.File)
            <span class="right">@Html.DropDownListFor(m => m.OwnedItemId, Model.Plans, "Not attached to a plan..")</span>
        </div>
        <div>
            @Html.EditorFor(m => m.Description,
                                new { @class = "addNotes", data_placeholders_focus = "false", placeholder = ViewData.ModelMetadata.Watermark })
            @Html.ValidationMessageFor(m => m.Description)
        </div>
    </div>
    <div class="addDEC">
        <input class="addDECButton" type="submit" value="Save" />
    </div>
}

视图模型:

public class AddDocumentViewModel
{
    [Display(Prompt = "Document notes")]
    [DataType(DataType.MultilineText)]
    [StringLength(500, ErrorMessage = "Must be 500 characters or less")]
    public string Description { get; set; }

    public int? OwnedItemId { get; set; }

    public IEnumerable<SelectListItem> Plans { get; set; }

    [Required(ErrorMessage = "Select a file")]
    public HttpPostedFileBase File { get; set; }

控制器

[HttpGet]
public ActionResult AddDocument()
{
    var partyId = (int)Session["PartyId"];
    var viewModel = _createBuilder.Build(partyId);
    return PartialView("_AddDocument", viewModel);
}

[HttpPost]
public ActionResult AddDocument(AddDocumentViewModel viewModel)
{
    var partyId = (int)Session["PartyId"];

    if (ModelState.IsValid)
    {
        _documentsManager.AddDocument(viewModel, partyId);

        if (Request.IsAjaxRequest())
            return Json(new { passedValidation = true, action = Url.Action("DocumentList") });

        return RedirectToAction("Index");
    }

    var newViewModel = _createBuilder.Rebuild(viewModel, partyId);
    return PartialView("_AddDocument", newViewModel);

}
4

0 回答 0