ASP.NET MVC5 HttpPostedFileBase始终返回null

时间:2016-06-02 10:52:12

标签: c# asp.net-mvc-5 azure-storage-blobs

我查了许多类似的问题,但无济于事。解决方案包括确保输入字段的名称与param相同,并包括 multipart / form-data。 POST时,图像始终返回null。

Confirm.cshtml

@model IQueryable<SGHealthDesktop.Models.Patient>

@using (Html.BeginForm("Confirm", "Doctor", new { enctype = "multipart/form-data", guid = Model.First().PatientId }, FormMethod.Post, null)) {
                    @Html.AntiForgeryToken()

                    <div class="form-horizontal">
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        @if (Model != null) {

                            <div class="form-group">
                                @Html.LabelFor(model => model.First().PatientName, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.First().PatientName, new { htmlAttributes = new { @class = "form-control", disabled = "disabled" } })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.First().UniqueId, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.First().UniqueId, new { htmlAttributes = new { @class = "form-control", disabled = "disabled" } })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.First().Evidence, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.TextBoxFor(model => model.First().Evidence, new { type = "file", name = "uploadFile", id = "uploadFile", required = "required", @class = "form-control" })
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-offset-2 col-md-10">
                                    <input type="submit" value="Request" class="btn btn-success" />
                                </div>
                            </div>
                        }
                    </div>
                }

DoctorController.cs

// POST: Doctor/Confirm
        [HttpPost, ActionName("Confirm")]
        [ValidateAntiForgeryToken]
        public ActionResult Confirmed(string guid, HttpPostedFileBase uploadFile) {
            try {
                if (ModelState.IsValid) { // to be binded later. 
                    ImageService imageService = new ImageService();
                    var imageUrl = imageService.UploadImage(UploadedFile);

                    var currentTime = DateTime.Now;

                    db.Appeal.Add(new Appeal {
                        DoctorId = User.Identity.GetUserId(),
                        PatientId = guid,
                        Evidence = imageUrl,
                        Time = currentTime, 
                        IsApprove = false
                    });
                    TempData["Message"] = "Appeal is being processed. Check the Request page for outcome.";
                    db.SaveChanges();
                    return RedirectToAction("Index");
                } else {
                    var errors = ModelState.Values.SelectMany(v => v.Errors);
                }
            } catch (DataException dex) {
            }
            return RedirectToAction("Patient");
        }

我可以知道我哪里出错吗?

更新

// GET: Doctor/Confirm
        public ActionResult Confirm(string id) {

            if (string.IsNullOrEmpty(id)) {
                return View("Error");
            }

            IQueryable<Patient> nameQuery = (from user in db.Users
                                             where user.Id.Equals(id)
                                             select new Patient {
                                                 PatientId = user.Id,
                                                 PatientName = user.FullName,
                                                 UniqueId = user.UniqueId
                                             });
            if (nameQuery == null) {
                return HttpNotFound();
            }
            return View(nameQuery);
        }

0 个答案:

没有答案