在MVC3绑定泛型列表到Dropdownlistfor [英] Binding a Generic List to a Dropdownlistfor in MVC3
问题描述
我有一个返回类别ID和类别名称泛型列表的方法。
我花了足够的时间研究和不能似乎把它在一起。我在MVC很新。
这里是一个仓库我DROPDOWNLIST方法。我找回数据......到目前为止好。
公开名单<&DROPDOWNLIST GT; GetDDl()
{返回catDDL;
}
这里是我的控制器code(尝试吧)
的IEnumerable< SelectListItem> LICAT =
。userRepository.Getddl()选择(C =>新建SelectListItem
{
值= c.DropDownID.ToString(),
文字= c.DropDownText
}
ViewBag.catItems =新SelecList(LICAT,值,文本);
这是我的看法
@ Html.Dropdownlist(catItems,选择类别)
尽量避免像动态的东西 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);
}
这篇关于在MVC3绑定泛型列表到Dropdownlistfor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!