DropDownListFor结合模型 [英] DropDownListFor binding to model

查看:87
本文介绍了DropDownListFor结合模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来asp.net MVC和尽管一些搜索不能成功地让我的头围绕在视图下拉列表。

具体我创建一个项目,首先是实体框架code。

我有要求用户有选择地挑选他们有牌照类型的表单。

和帮助或援助将大大AP preciated。我期待在视图中创建一个DropDownListFor帮手,使下拉列表中LicenceType。

有关许可证类型的GRCMember模型部分是:

 公共类GRCMember
{
    [专栏(类型名=INT)]
    公众诠释? LicenceTypeId {搞定;组; }
}

该牌照类型型号是:

 公共类LicenceType
{
    公众诠释LicenceTypeId {搞定;组; }
    [StringLength(150)]
    [专栏(类型名=nvarchar的)]
    公共字符串类型{搞定;组; }
    [专栏(类型名=位)]
    公共BOOL?蛰伏{搞定;组; }
    公共虚拟的ICollection< GRCMember> GRCMembers {搞定;组; }
}

相关寄存器视图模型部分:

 公共类RegisterViewModel
{
    [显示(NAME =牌照类型)]
    公众诠释? SelectedLicenceTypeId {搞定;组; }
}

的get控制器的方法是:

  [使用AllowAnonymous]
[HTTPGET]
公众的ActionResult寄存器()
{
    返回查看();
}

最后下拉视图的部分:

  @model GRCWebApp.Models.RegisterViewModel
< D​​IV CLASS =COL-MD-3 COL-MD-偏移1>
    @ Html.LabelFor(型号=> model.SelectedLicenceTypeId,htmlAttributes:新{@class =控制标签})
    @ Html.EditorFor(型号=> model.SelectedLicenceTypeId,新{htmlAttributes = {新@class =表格控}})
    @ Html.ValidationMessageFor(型号=> model.SelectedLicenceTypeId,新{@class =TEXT-危险})
< / DIV>


解决方案

让我们假设你有两个实体称为Person和城市。然后,你会为了填充模型中的这两个实体使用视图模型如下图所示:

PersonViewModel:

 公众人物的人获得{;组; }
公共IEnumerable的<城市>城市{搞定;组; }



当然,控制器返回视图模型:

控制器:

 公开的ViewResult的Create()
{
    PersonViewModel模式=新PersonViewModel
    {
        人=新的Person()
        城市= repository.Cities.ToList()
    };
    返回查看(模型);
}



你应该通过视图模型到您的视图,然后使用DROPDOWNLIST,如下图所示:

查看:

  @model Project.WebUI.Models.PersonViewModel
...
@ Html.LabelFor(M = GT; m.Person.City)
@ Html.DropDownListFor(M => m.Person.City,新的SelectList(Model.CitiesCityID,CITYNAME),----选择----)

希望这有助于...

I am new to asp.net mvc and despite several searches can't get my head successfully around drop down lists in a view.

Specifically I'm creating a project that is entity framework code first.

I have a form that requires the user to optionally pick the Licence Type they have.

And help or assistance would be greatly appreciated. I'm looking to create a DropDownListFor helper in the view to enable the drop down list for LicenceType.

The GRCMember model section relating to licence type is:

public class GRCMember
{
    [Column(TypeName = "int")]
    public int? LicenceTypeId { get; set; }
}

The Licence Type Model is:

public class LicenceType
{
    public int LicenceTypeId { get; set; }
    [StringLength(150)]
    [Column(TypeName = "nvarchar")]
    public String Type { get; set; }
    [Column(TypeName = "bit")]
    public bool? Dormant { get; set; }
    public virtual ICollection<GRCMember> GRCMembers { get; set; }
}

The associated Register View Model section is:

public class RegisterViewModel
{
    [Display(Name = "Licence Type")]
    public int? SelectedLicenceTypeId { get; set; }
}

The get controller method is:

[AllowAnonymous]
[HttpGet]
public ActionResult Register()
{
    return View();
}

And finally the drop down section of the view is:

@model GRCWebApp.Models.RegisterViewModel
<div class="col-md-3 col-md-offset-1">
    @Html.LabelFor(model => model.SelectedLicenceTypeId, htmlAttributes: new { @class = "control-label" })
    @Html.EditorFor(model => model.SelectedLicenceTypeId, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.SelectedLicenceTypeId, "", new { @class = "text-danger" })
</div>

解决方案

Let's assume that you have two entities called Person and City. Then you would use a ViewModel in order to populate these two entities in a model as shown below:

PersonViewModel:

public Person Person { get; set; }
public IEnumerable<City> Cities { get; set; }


The controller of course returns the ViewModel:

Controller:

public ViewResult Create()
{
    PersonViewModel model = new PersonViewModel
    {
        Person = new Person (),
        Cities = repository.Cities.ToList()
    };
    return View(model);
}


You should pass the ViewModel to your View and then use the Dropdownlist as shown below:

View:

@model Project.WebUI.Models.PersonViewModel
...
@Html.LabelFor(m => m.Person.City)
@Html.DropDownListFor(m => m.Person.City, new SelectList(Model.Cities, "CityID", "CityName"), "---- Select ----")

Hope this helps...

这篇关于DropDownListFor结合模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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