且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MVC3绑定泛型列表到Dropdownlistfor

更新时间:2023-02-25 19:41:06

尽量避免像动态的东西 ViewBag 的ViewData ​​ code>。使用强类型的意见。

视图模型只是我们将使用到您的视图和操作方法之间传输数据的POCO类。这将是特定于视图。

例如:如果你想创建它创建了一个产品的视图。因此,创建一个这样的视图模型

 公共类产品
{
  公共字符串名称{集;获取;}
  公共IEnumerable的< SelectListItem>分类{搞定;组; }
  公共字符串SelectedCategoryId {搞定;组; }
  //根据需要其他属性}

现在在你的 GET 的操作方法,你创建这个视图模型的对象并初始化值,并发送到视图。

 公众的ActionResult的Create()
{
  VAR VM =新产品();
  vm.Categories = userRepository.Getddl()。
               选择(C =>新建SelectListItem
                                 {
                                    值= c.DropDownID.ToString(),
                                    文字= c.DropDownText
                                 });
  返回查看(VM);
}

现在使你的观点强类型我们的产品类,并使用 Html.DropDownListFor helper方法。

  @model PersonsProduct
@using(Html.BeginForm())
{
  @ Html.DropDownListFor(X => x.SelectedCategoryId,
                       新的SelectList(Model.Categories,值,文本),选择)
  <输入类型=提交值=拯救/>
}

现在在你的HttpPost,你可以得到表单值这样

  [HttpPost]
公众的ActionResult创建(产品型号)
{
  如果(ModelState.IsValid)
  {
     //检查model.SelectedCategoryId
     //保存和重定向
  }
  //做:重新加载下拉。
  返回视图(模型);
}

I have a generic list method that returns a CategoryID and CategoryName. I have spent enough time researching and cant seem to put it together. I very new at MVC.

Here is my DropdownList Method in a repository. I get back the data... So far so good.

public List<DropdownList> GetDDl()
{

return catDDL; 
}

Here is my CONTROLLER CODE(attempt at it)

   IEnumerable<SelectListItem> liCat =
 userRepository.Getddl().Select(c => new SelectListItem
{
   Value = c.DropDownID.ToString(),
   Text = c.DropDownText
}
ViewBag.catItems = new SelecList(liCat,"Value","Text");

Here is my VIEW

@Html.Dropdownlist("catItems","Select Category)

Try to avoid dynamic stuff like ViewBag and ViewData. Use strongly typed views.

ViewModel is just a POCO class which we will use to transfer data between your view and the action method. It will be specific to the view.

ex : if you want to create a view which creates a product. So create a viewmodel like this

public class Product
{
  public string Name { set;get;}
  public IEnumerable<SelectListItem> Categories{ get; set; }
  public string SelectedCategoryId { get; set; }
  //Other Properties as needed

}

now in your GET action method, you create an object of this view model and initialize the values and send to the view.

public ActionResult Create()
{
  var vm=new Product();
  vm.Categories=userRepository.Getddl().
               Select(c => new SelectListItem
                                 {
                                    Value = c.DropDownID.ToString(),
                                    Text = c.DropDownText
                                 });                    
  return View(vm);
}

Now make your view strongly typed to our Product class and use the Html.DropDownListFor helper method.

@model PersonsProduct 
@using (Html.BeginForm())
{ 
  @Html.DropDownListFor(x => x.SelectedCategoryId, 
                       new SelectList(Model.Categories,"Value","Text"), "Select")
  <input type="submit" value="save" />
}

Now in your HttpPost , you can get the form values like this

[HttpPost]
public ActionResult Create(Product model)
{
  if(ModelState.IsValid)
  {
     //check model.SelectedCategoryId
     //save and redirect
  }
  //to do :reload the dropdown again.
  return view(model);
}