返回到Controller中的Action时,POSTed DDL列表为空

时间:2012-03-17 10:53:53

标签: c# asp.net-mvc

我正在创建一个具有DropDown的View。该视图提供了一个类似于以下内容的CategoryModel:

public class CategoryModel
{
    [Required]
    [Display(Name = "Categories")]
    public List<Category> Categories { get; set; }

    [Required]
    [GreaterThan(ExceedValue = 0, ErrorMessage = "Please select a category.")]
    [Display(Name = "SelectedCategoryId")]
    public int SelectedCategoryId { get; set; }
}

在视图中使用类别列表来填充DropDown,首先获取类别并将它们放入SelectList中,如下所示:

@model RatingMVC3.Models.CategoryModel

@{
Layout = "~/Views/Shared/_MainLayout.cshtml";
ViewBag.Title = "Upload";
}

<h2>Upload</h2>

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
    @Html.DropDownListFor(m => m.SelectedCategoryId, new SelectList(Model.Categories, "Id", "Description"), "-- Select Category --");
    @Html.ValidationMessageFor(m => m.SelectedCategoryId);
    @Html.HiddenFor(m => m.Categories);
}

当提交此表单并将模型返回给Controller时,我可以看到模型返回到Controller中,但它包含一个空List而不是View中的List列表。 (SelectedCategoryId符合预期)。这是Controller中的ActionResult方法:

    [HttpPost]
    [Authorize]
    public ActionResult Upload(HttpPostedFileBase file, CategoryModel model)
    {
        if (ModelState.IsValid)
        {
            if (file != null && file.ContentLength > 0)
            {
                var FileExtension = Path.GetExtension(file.FileName);
                string Path1 = null;
                string FileName = null;
                do
                {
                    var randomName = Path.GetRandomFileName();
                    FileName = Path.ChangeExtension(randomName, FileExtension);
                    Path1 = Path.Combine(Server.MapPath("~/Images"), FileName);
                } while (System.IO.File.Exists(Path1));
                file.SaveAs(Path1);
                if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId))
                {
                    return RedirectToAction("Uploaded", "Upload");
                }
            }
            return RedirectToAction("Index", "Home");
        }

        return View(model);
    }

空列表对我来说是个问题,因为正如您所看到的,如果ModelState无效,视图将再次返回相同的模型,并且需要填充类别。 我希望有人可以回答这个问题;)如果需要,我很乐意指定更多信息,提前致谢

1 个答案:

答案 0 :(得分:0)

在POST操作期间,整个选择列表(下拉列表)不会返回到控制器/表单操作,只返回该列表中的选定值。如果由于验证错误而必须重新渲染视图,则可以a)执行AJAX提交而不是完整POST以获得验证原因,或者b)重新创建列表并使用View on Error将其发回。选项b)可以这样实现:

[HttpPost]
    [Authorize]
    public ActionResult Upload(HttpPostedFileBase file, CategoryModel model)
    {
        if (ModelState.IsValid)
        {
            if (file != null && file.ContentLength > 0)
            {
                var FileExtension = Path.GetExtension(file.FileName);
                string Path1 = null;
                string FileName = null;
                do
                {
                    var randomName = Path.GetRandomFileName();
                    FileName = Path.ChangeExtension(randomName, FileExtension);
                    Path1 = Path.Combine(Server.MapPath("~/Images"), FileName);
                } while (System.IO.File.Exists(Path1));
                file.SaveAs(Path1);
                if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId))
                {
                    return RedirectToAction("Uploaded", "Upload");
                }
            }
            return RedirectToAction("Index", "Home");
        }
        //I don't know how you fetch this list, but just do it again before returning
        //the view
        model.Categories = GetMyCategories();
        return View(model);
    }

我想如果你愿意的话,你可以将项目列表存储为页面上的隐藏输入,但这看起来很丑陋,增加了发送到网页和从网页发送的数据量,并且gosh darn it,听起来很像ViewState(shrudders)。