当使用enctype =“multipart / form-data”表单时不发布

时间:2013-03-18 02:26:46

标签: asp.net-mvc-3 c#-4.0 asp.net-mvc-4

我正在开发ASP.Net MVC 4上的应用程序,我正在尝试上传表单中的图像。我遇到的问题是,当表单发布时,如果<input type="file"...为空(意味着我没有选择文件),表单发布就好了,一切正常。但是,当我选择一个文件时,表单只是坐在那里而什么都不做。

起初我以为它只是花了一段时间上传,但我已经离开了很长一段时间(文件大小是7kb)而且没有。调试器甚至没有在操作开始时触发断点。我很茫然,因为我对这个平台很陌生,而且我每天都在学习。请在下面找到所有相关代码:

视图:

 @using (Html.BeginForm("StaffInformation", "Manager", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data"}))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
                <div class="control-group">
                    @Html.LabelFor(model => model.firstName, new { @class = "control-label" })
                    <div class="controls">
                        @Html.TextBoxFor(model => model.firstName, new { @class = "span12" })
                        @Html.ValidationMessageFor(model => model.firstName)
                    </div>
                </div>

                <div class="control-group">
                    @Html.LabelFor(model => model.lastName, new { @class = "control-label" })
                    <div class="controls">
                        @Html.TextBoxFor(model => model.lastName, new { @class = "span12" })
                        @Html.ValidationMessageFor(model => model.lastName)
                    </div>
                </div>

                <div class="control-group">
                    Staff Photo:
                    <div class="controls">
                        @if (Model.staffImage == null)
                        {
                            @:None
                        }else{
                        <img width="150" height="150" src="@Url.Action("GetImage", "Manager", new { Model.staffProfileID })" /><br />
                        }
                        <input type="file" id="staffImage" name="staffImage" data-provide="fileinput">
                    </div>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">Save changes</button>
                    <a href="@Url.Action("dashboard", "manager")" class="btn" type="reset" >Cancel</a>
                </div>
            }

行动:

    public ActionResult StaffInformation(StaffInformationViewModel model, HttpPostedFileBase staffImage)
    {
    if (ModelState.IsValid)   //This is where the breakpoint is
        {
            if (staffImage != null) {
                model.imageMimeType = staffImage.ContentType;
                model.staffImage = new byte[staffImage.ContentLength];
                staffImage.InputStream.Read(model.staffImage, 0, staffImage.ContentLength);
            }

            using (var repo = new CompanyInformationRepository(new UniteOfWorkCompanies()))
            {
                var company = repo.FindCompany(User.Identity.Name);

                repo.AddOrUpdateStaff(model, company);
            }

            return RedirectToAction("ManageStaff");
        }
    }

我真的不知道发生了什么,只是我使用enctype = "multipart/form-data"的原因是因为我正在阅读Adam Freeman的“Pro ASP.NET MVC 4”并且在那里他们说它没有它就行不通。就像我说的,我很新,需要我能得到的所有帮助。

1 个答案:

答案 0 :(得分:2)

您可以尝试将HttpPostedFileBase staffImage作为ViewModel的一部分(StaffInformationViewModel) - 同样,您需要将enctype = "multipart/form-data"保留在表单标记中:

public class StaffInformationViewModel
{
    // your other properties here ...

    [Required]
    public HttpPostedFileBase StaffImage { get; set; }
}

然后在视图中:

@Html.TextBoxFor(model => model.StaffImage, new { type = "file" })

然后在Controller中:

    public ActionResult StaffInformation(StaffInformationViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (model.StaffImage != null)
            {
                // awesome
            }
            // ... more awesome
        }
    }

我希望这会有所帮助。如果您需要更多关于此问题的澄清,请告诉我......

相关问题