如何使用razor获取MVC 3中的dro down列表值 [英] How to fetch a dro down list value in MVC 3 using razor

查看:52
本文介绍了如何使用razor获取MVC 3中的dro down列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ Html.DropDownListFor(model => model.name,new SelectList(new List< Object> {

new {value =gg,text = name1},

new {value =hh,text = name2},

new {value =jj,text = name3},

new {value = kk,text = name4}

},value,text),new {id =ddlClass,@ class =field04})

解决方案

嗨哥们,



你只需要做下面的事情,



在您的Model类中创建一个属性



public List< selectlistitem>姓名{get; set;}



然后只需在模型类中创建方法



公共列表< selectlistitem> GetNames()

{

var names = new List< selectlistitem>();



名称。 add  new  SelectListItem {
Text = 1
Value = < span class =code-string> 1});





名称。 add (new SelectListItem {
Text = 1
值= 1});







名称。 add (new SelectListItem {
Text = 1
值= 1});





返回姓名;

}





然后在模型的构造函数中



public ModelClassName()

{

Names = GetNames();

}



然后,打电话这个型号来自控制器,并通过模型查看





现在你只需要在视野中正确



 @ Html.DropDownListFor(model = >  model.name, model.Names  new  {id =   ddlClass,@ class =   field04})









如果您觉得这很有用,请投票支持。

谢谢。


@Html.DropDownListFor(model => model.name, new SelectList(new List<Object>{
new { value = "gg" , text = name1 },
new { value = "hh" , text = name2 },
new { value = "jj" , text = name3},
new { value = "kk" , text = name4 }
}, "value", "text"), new { id = "ddlClass", @class = "field04" })

解决方案

Hi buddy,

You just need to do below thing,

Create a property in your Model class

public List<selectlistitem> Names{get;set;}.

Then just create method in model class

Public List<selectlistitem> GetNames()
{
var names=new List<selectlistitem>();

names.add(new SelectListItem{
              Text="1",
              Value="1"});



names.add(new SelectListItem{
              Text="1",
              Value="1"});




names.add(new SelectListItem{
              Text="1",
              Value="1"});



return names;
}


Then in constructor of model

public ModelClassName()
{
Names=GetNames();
}

Then, call this model from controller and pass the model to view


Now you just have to right in view

@Html.DropDownListFor(model => model.name, model.Names, new { id = "ddlClass", @class = "field04" })





If you find this useful then please vote for this.
Thanks.


这篇关于如何使用razor获取MVC 3中的dro down列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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