在ASP.NET MVC5中绑定@ Html.DropDownListFor的最佳方法是什么? [英] What is the best ways to bind @Html.DropDownListFor in ASP.NET MVC5?

查看:249
本文介绍了在ASP.NET MVC5中绑定@ Html.DropDownListFor的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用Viewbag的情况下从Model数据绑定@Html.DropDownListFor,并在网络上查看许多不同的示例.但是他们大多数使用Viewbag或扩展方法,我希望有一种更好的方法来解决此问题.我尝试了以下方法,但似乎不起作用:

I want to bind @Html.DropDownListFor from Model data without using Viewbag and look at many different examples on the web. But most of them use Viewbag or an extension method and I want a better approach to solve this problem. I tried the the following methods but it does not seem to work:

@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Lab, "Id", "Name"), 
    "---- Select----", new { @class = "selectpicker" } )

是否可以不使用Viewbag或ASP.NET MVC5Controller中的任何其他方法直接从模型绑定@Html.DropDownListFor?您能否举一些例子来达到最佳效果?

Is it possible to bind @Html.DropDownListFor directly from Model without using Viewbag or any extra method in the Controller in ASP.NET MVC5? Could you please give some examples to perform this best?

推荐答案

强类型视图模型方法,它不使用ViewBag之类的动态内容

您可以为类型为SELECT的类型的视图模型添加新属性. IEnumrable<SelectListItem>.

You can add a new property to your view model for the SELECT options of type IEnumrable<SelectListItem>.

视图模型是一个简单的POCO类,用于在视图到动作方法之间进行数据传输,反之亦然.它们是特定于视图的.添加仅视图所需的属性.

public class CreateUserVm
{
   public IEnumrable<SelectListItem> Labs { set;get;}
   public int SelectedLabId { set;get;}
   //Add other properties as needed for the view
}

并在您的GET操作中,创建此视图模型的对象,加载Labs属性并将其发送到视图.

and in your GET action, create an object of this view model, load the Labs property and send that to the view.

public ActionResult Create()
{
  var vm= new CreateUserVm();
  // Hard coded for demo. You can replace with real data from db
  vm.Labs = new List<SelectListItem> {
     new SelectListItem { Value="1", Text="One" },
     new SelectListItem { Value ="2", Text="Two" }
  };
  return View(vm);
}

,然后在该视图模型的强类型视图中,调用DropDownListFor helper方法

and in the view which is strongly typed to this view model, call the DropDownListFor helper method

@model CreateUserVm
@Html.DropDownListFor(f=>f.SelectedLabId, Model.Labs,"Select one")

在下拉菜单中预选择一个选项

如果您希望在剃刀渲染页面时预先选择一个选项,则可以将视图模型的SelectedLabId属性值设置为Option项(SelectListItem)的value属性值.

If you like to pre select one option when razor renders the page, You can set the SelectedLabId property value of your view model to the value property value of of the Option item(SelectListItem).

public ActionResult Create()
{
  var vm= new CreateUserVm();
  // Hard coded for demo. You can replace with real data from db
  vm.Labs = new List<SelectListItem> {
     new SelectListItem { Value="1", Text="SugarLab" },
     new SelectListItem { Value ="2", Text="CandyLab" },
     new SelectListItem { Value ="3", Text="SodaLab" }
  };
  vm.SelectedLabId = 2; // Will set "CandyLab" option as selected
  return View(vm);
}

如果您要使用真实数据,而不是硬编码的2个项目,则可以这样做

If you want to use real data, instead of the hard coded 2 items, you can do this

vm.Labs = dbContext.Labs.Select(x=>new SelectListItem { Value=x.Id.ToString(),
                                                        Text= x.Name }).ToList();

假定dbContext是您的DbContext类对象,并且它具有类型为DbSet<Lab>Labs属性,其中每个Lab实体都具有Id和Name属性.

Assuming dbContext is your DbContext class object and it has a Labs property of type DbSet<Lab> where each Lab entity has an Id and Name property.

这篇关于在ASP.NET MVC5中绑定@ Html.DropDownListFor的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆