asp.net-mvc ASP.NET MVC 下拉编辑器模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4014428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC DropDown Editor Template
提问by Craig
I am looking for the best method for creating a drop down list editor template with MVC. There seem to be various methods but I can't find any method that is best, everyone seems to do it differently. I am using MVC3 with Razor as well, so a method that works with this is preferred.
我正在寻找使用 MVC 创建下拉列表编辑器模板的最佳方法。似乎有各种各样的方法,但我找不到最好的方法,每个人似乎都不一样。我也将 MVC3 与 Razor 一起使用,因此首选使用这种方法的方法。
回答by Darin Dimitrov
There are many ways and saying which is the best would be subjective and might not work in your scenario which by the way you forgot to describe in your question. Here's how I do it:
有很多方法,说哪种最好是主观的,并且可能不适用于您忘记在问题中描述的场景。这是我的方法:
Model:
模型:
public class MyViewModel
{
public string SelectedItem { get; set; }
public IEnumerable<Item> Items { get; set; }
}
public class Item
{
public string Value { get; set; }
public string Text { get; set; }
}
Controller:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// TODO: Fetch this from a repository
Items = new[]
{
new Item { Value = "1", Text = "item 1" },
new Item { Value = "2", Text = "item 2" },
new Item { Value = "3", Text = "item 3" },
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
// redisplay the view to fix validation errors
return View(model);
}
// TODO: The model is valid here =>
// perform some action using the model.SelectedItem
// and redirect to a success page informing the user
// that everything went fine
return RedirectToAction("Success");
}
}
View (~/Views/Home/Index.cshtml
):
视图 ( ~/Views/Home/Index.cshtml
):
@model MyApp.Models.MyViewModel
@{ Html.BeginForm(); }
@Html.EditorForModel()
<input type="submit" value="OK" />
@{ Html.EndForm(); }
Editor template (~/Views/Home/EditorTemplates/MyViewModel.cshtml
):
编辑器模板 ( ~/Views/Home/EditorTemplates/MyViewModel.cshtml
):
@model MyApp.Models.MyViewModel
@Html.DropDownListFor(x => x.SelectedItem,
new SelectList(Model.Items, "Value", "Text"))
回答by Wahid Bitar
This is my approach from this post:
这是我从这篇文章中得到的方法:
回答by Fran?ois Camus
Personally I think that list items should be placed in the view data not the view model but it really depends if you are displaying a drop down that never changes (using view data) or if you'll have to modify it dynamically (using a view model).
就我个人而言,我认为列表项应该放在视图数据中而不是视图模型中,但这实际上取决于您是显示一个永远不会改变的下拉列表(使用视图数据)还是必须动态修改它(使用视图模型)。
In the example you are posting the same view model to the index action. The index action is only interested in the selected item so could just change the parameter of the index post action to be string selectedItem. That way the model binder will look into the form parameters and populate the index parameter for you.
在示例中,您将相同的视图模型发布到索引操作。索引操作仅对所选项目感兴趣,因此可以将索引发布操作的参数更改为字符串 selectedItem。这样,模型绑定器将查看表单参数并为您填充索引参数。
Also, I think it would be better to pass a list of SelectedListItems down to the view that way you wouldn't need any convertion and wouldn't need the Item class.
另外,我认为最好将 SelectedListItems 列表传递给视图,这样您就不需要任何转换并且不需要 Item 类。