【问题标题】:MVC3 - Accessing contents of dropdownlist in ControllerMVC3 - 访问控制器中下拉列表的内容
【发布时间】:2011-06-12 11:12:43
【问题描述】:

我是 MVC2/3 的新手,所以请记住这一点。此外,也不能使用 Ajax 或 jQuery。

我有一个网页,用户必须从下拉列表中选择一个项目,然后点击“过滤器”按钮。 (单击此按钮将简单地触发我的控制器中的默认 POST 操作并返回过滤后的结果列表。

除了遇到一个问题外,我一切正常。当 Filter 操作完成并将控制权返回给我的视图时,下拉列表内容将丢失(即 null)。结果返回没有问题,只是我的下拉列表是空白的 - 从而阻止用户从列表中选择另一个项目。

我应该重新填写过滤操作的下拉列表还是有更简洁的方法来做到这一点?

这是我的代码快照:

我的视图模型

public class MyViewModel {
        [DisplayName("Store")]
        public IEnumerable<Store> StoreList { get; set; }

        public string SelectedStore { get; set; }
}

我的视图 (Index.cshtml)

@using (Html.BeginForm()) {

    <h2>Search</h2>

    @Html.LabelFor(m => m.StoreList)
    @Html.DropDownListFor(m => m.SelectedStore, new SelectList(Model.StoreList, "StoreCode", "StoreCode"), "Select Store")

    <input type="submit" value="Filter" />
}

我的控制器:

public class MyController : Controller
{
        public ActionResult Index() {

            MyViewModel vm = new MyViewModel();
            var storelist = new List<Store>();
            storelist.Add(new Store { StoreCode = "XX" });
            storelist.Add(new Store { StoreCode = "YY" });
            storelist.Add(new Store { StoreCode = "ZZ" });
            vm.StoreList = storelist;

            return View(vm);
        }

        [HttpPost]
        public ActionResult Index(MyViewModel model, string SelectedStore, FormCollection collection) {

            if (ModelState.IsValid) {
                /* this works, model state is valid */

                /* BUT, the contents of model.StoreList is null */
            } 

            return View( model);
        }
}

【问题讨论】:

    标签: asp.net-mvc-2 controller html.dropdownlistfor asp.net-mvc-3


    【解决方案1】:

    是的,您必须重新填充任何传递给视图的模型(包括 ViewData)。请记住,这是一个无状态系统,每次调用都会重新实例化您的控制器,并从头开始有效。

    我会这样做:

    public class MyController : Controller
    {
         private List<Store> GetStoreList()
         {
              List<Store> StoreList = new List<Store>();
              // ... Do work to populate store list
              return StoreList;
         }
    
         public ActionResult Index() {
    
             MyViewModel vm = new MyViewModel();
             vm.StoreList = GetStoreList();
             return View(vm);
         }
    
         [HttpPost]
         public ActionResult Index(MyViewModel model, string SelectedStore, FormCollection collection) {
    
             if (ModelState.IsValid) {
                /* this works, model state is valid */
    
                /* BUT, the contents of model.StoreList is null */
             } 
             model.StoreList = GetStoreList();
             return View( model);
         }
    }
    

    【讨论】:

      【解决方案2】:

      简短的回答是肯定的,您需要重新填写过滤操作中的下拉列表。 ASP.NET MVC 不是 WebForms - 没有 ViewState 来保留列表的内容。

      【讨论】:

        【解决方案3】:

        再次填写下拉菜单,因为 mvc 没有查看状态

        [HttpPost] public ActionResult Index(MyViewModel model, string SelectedStore, FormCollection collection) {

             if (ModelState.IsValid) {
                /* this works, model state is valid */
        
                /* BUT, the contents of model.StoreList is null */
             } 
             model.StoreList = GetStoreList();
             return View( model);
         }
        

        【讨论】:

          猜你喜欢
          • 2017-09-05
          • 2012-05-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-18
          • 2012-07-09
          • 2017-03-11
          相关资源
          最近更新 更多