从视图模型实现DROPDOWNLIST在Asp.net MVC 3 [英] Implementing Dropdownlist on Asp.net MVC 3 from viewModel

查看:111
本文介绍了从视图模型实现DROPDOWNLIST在Asp.net MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.NET和MVC平台,我有一些存储的DropDownList值这么多int字段中,我创建领域诠释由于类型数据库大小所以我通过这种方法实现的DropDownList,它的工作原理,但我不知道这是否是正确的解决方案在这里的ViewModel值的存储文本是示例code:

我为我的英语很抱歉,请让我知道,如果你什么都不懂。

模式property.cs

  [必需(的ErrorMessage =卜艾伦gereklidir!)]
    [显示(NAME =Şehir),最大长度(60)]
    公众诠释市{搞定;组; }

propertymgmtviewModel

 公共房屋物业{搞定;组; }
    公共IEnumerable的<性>属性{搞定;组; }
    公共IEnumerable的< SelectListItem>城市
    {
        得到
        {
            返回新的[]
            {
            新SelectListItem的{text = - 选择一个---值=,选择= TRUE},
            新SelectListItem的{text =芝加哥,值=1},
            新SelectListItem的{text =纽约,值=2},
            新SelectListItem的{text =津巴布韦,值=3},
            };
        }

视图编辑视图页:

  @ Html.DropDownListFor(型号=> model.Property.City,
        新的SelectList(Model.Cities,值,文本))

显示值的ViewPage:

 < TD>
   @(item.City!= NULL?
   Model.Cities.SingleOrDefault(C => c.Value == item.City.ToString())。文本
   :)        < / TD>


解决方案

如果我是你,我应该使用一个字典的键/显示名这样的选择列表:

一个静态成员(因为它是静态的...):

 静态字典< INT,串> CitiesDict =新词典< INT,串>()
    {
        {-1 - 选择一个---},
        {0,芝加哥},
        {1,纽约},
        {2,津巴布韦},
    };

有关下拉一个属性:

 公开的SelectList城市{搞定;组; }

在初始化构造是这样的:

 城市=新的SelectList(CitiesDict,键,值,-1);

然后在编辑观点:

  @ Html.DropDownListFor(型号=> model.Property.City,Model.Cities)

和用于显示:

  @(CitiesDict.ContainsKey(Model.City)CitiesDict [Model.City]:)

I'm new to .net and mvc platform, i have so many int fields that stores some dropdownlist values, i've created fields int type due to database size so i'm implementing dropdownlist via this method, it works but i don't know if it's the correct solution to store text of values on viewmodel here is the sample code:

i'm so sorry for my english, please let me know if you don't understand anything.

model property.cs

    [Required(ErrorMessage = "Bu alan gereklidir!")]
    [Display(Name = "Şehir"), MaxLength(60)]
    public int City { get; set; }

propertymgmtviewModel

 public Property Property { get; set; }
    public IEnumerable<Property> Properties { get; set; }
    public IEnumerable<SelectListItem> Cities
    {
        get
        {
            return new[]
            {
            new SelectListItem { Text="--Select One---", Value = "", Selected=true},
            new SelectListItem { Text="Chicago", Value = "1"},
            new SelectListItem { Text="New York", Value = "2"},
            new SelectListItem { Text="Zimbabwe", Value = "3"},
            };
        }

view for editing view page:

 @Html.DropDownListFor(model => model.Property.City,
        new SelectList(Model.Cities,"Value","Text"))

displaying values viewpage :

   <td>
   @(item.City != null ?
   Model.Cities.SingleOrDefault(c => c.Value == item.City.ToString()).Text
   : "")

        </td>

解决方案

If I was you, I should use a dictionary key / displayName like this for the SelectList :

one static member (as it is static...) :

static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
    {
        { -1 , "--Select One---"},
        { 0 ,"Chicago"},
        { 1 ,"New York"},
        { 2 ,"Zimbabwe"},
    };

One Property for the dropdown :

public SelectList Cities { get; set; }

Initialized in constructor like this :

Cities = new SelectList(CitiesDict , "Key", "Value", -1);

Then in the view for editing :

@Html.DropDownListFor(model => model.Property.City, Model.Cities) 

And for displaying:

@(CitiesDict.ContainsKey(Model.City) ? CitiesDict[Model.City] : "")

这篇关于从视图模型实现DROPDOWNLIST在Asp.net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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