如何从mvc3中的下拉列表中获取价值? [英] How to get value from dropdownlist in mvc3 ?

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

问题描述

 public ActionResult bindddl()
{
List < CRBT_SongMaster > titlelist = new List < CRBT_SongMaster > ();
var TitleLST = new List < string > ();
var GenreQuery =来自z的dbcontext.CRBT_SongMaster orderby z.SongId select z.TitleAlbum;
TitleLST.AddRange(GenreQuery.Distinct());
ViewBag.TitleList = new SelectList(TitleLST);
return View(bindddl,titlelist);
}







这是我上面的行动方法。



以下是我的查看页面....



专辑列表



@Html.DropDownList(TitleList,----选择专辑标题----)



请帮帮我我将如何从此下拉列表中获取价值?

解决方案

在后期操作中尝试此操作。



< pre lang =c#>专辑.title = Convert.ToInt32(请求[ TitleList]) ;


@ Html.DropDownListFor(Model => Model.AlbumTitle,new List< selectlistitem>

{

new SelectListItem {Text =Equities,Value =5},

new SelectListItem {Text =Bonds,Value =1},
new SelectListItem {Text =CDS,Value =2},

new SelectListItem {Text =ADS,Value =3},

new SelectListItem {Text =EDC,Value =4}

}, - 选择专辑 - ,新{style =width:153px;})< br $>








[HttpPost]

public ActionResult bindddl(Model objModel)

{

//这里获取下拉列表值

string str = objModel.AlbumTitle;



}


 < span class =code-keyword>在控制器
ViewBag.StateList =(来自 s in _db.StateMasters
选择 s).ToList();

查看
< div class = control-group>
< div class = controls>
< div class = form-label>
状态 star> * < / div> ;
@ Html .DropDownListFor(model => model.State,
new SelectList(ViewBag.StateList, StateName StateName ), - 选择 - ,new { @ class = span12 cusmo-input})
@ Html .ValidationMessage( State,new { @ style = 颜色:红色})
< / div>

< / div>

控制器

objCust.State = model.State == null ? string.Empty:model.State;


public ActionResult bindddl()
       {
           List<CRBT_SongMaster> titlelist = new List<CRBT_SongMaster>();
           var TitleLST = new List<string>();
           var GenreQuery = from z in dbcontext.CRBT_SongMaster orderby z.SongId select z.TitleAlbum;
               TitleLST.AddRange(GenreQuery.Distinct());
               ViewBag.TitleList = new SelectList(TitleLST);
              return View("bindddl", titlelist);
       }




This is my action method above.

below is my view page....

Album List


@Html.DropDownList("TitleList", "----Select Album Title----")

please help me how will I get value from this dropdownlist ?

解决方案

try with this in post action.

Album .title = Convert.ToInt32(Request["TitleList"]);


@Html.DropDownListFor(Model=>Model.AlbumTitle, new List<selectlistitem>
{
new SelectListItem{ Text="Equities", Value = "5" },
new SelectListItem{ Text="Bonds", Value = "1" },
new SelectListItem{ Text="CDS", Value = "2" },
new SelectListItem{ Text="ADS", Value = "3" },
new SelectListItem{ Text="EDC", Value = "4" }
}, "--Select Album--",new { style = "width: 153px;" })




[HttpPost]
public ActionResult bindddl( Model objModel)
{
//Here Get Dropdown list value
string str=objModel.AlbumTitle;

}


In Controller
ViewBag.StateList = (from s in _db.StateMasters
                                select s).ToList();

In View
 <div class="control-group">
                        <div class="controls">
                            <div class="form-label ">
                                State "star">*</div>
                            @Html.DropDownListFor(model => model.State,
                                      new SelectList(ViewBag.StateList, "StateName", "StateName"), "-- Select --", new { @class = "span12 cusmo-input" })
                            @Html.ValidationMessage("State", new { @style = "color:red" })
                        </div>
      
              </div>

In controller

 objCust.State = model.State == null ? string.Empty : model.State;


这篇关于如何从mvc3中的下拉列表中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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