在MVC3中获取选定的下拉列表框值 [英] Get selected dropdown list boxvalue in MVC3
本文介绍了在MVC3中获取选定的下拉列表框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是MVC3的新手。我在View中创建了一个静态下拉列表框
Hi,
I am new to MVC3. I have created a static dropdown list box in View
<div class ="editor-field">
<select name="quota" id="list">
<option value="1">General</option>
<option value="2">Tatkal</option>
<option value="3">Ladies</option>
</select>
</div>
现在我想使用ViewBag将所选值传递给另一个页面。
我不知道我需要什么东西有模型和控制器页面。
请帮忙。
Now I want to pass the selected value to another page using ViewBag.
I don't know what are the things I need to have in Model and Controller pages.
Please help.
推荐答案
我认为最好的方式哟通过使用ViewModel实现这一点。
创建两个类QuotaViewModel和配额
I think that the best way yo achieve this by using ViewModel.
Create two class QuotaViewModel and Quota
public class QuotaViewModel
{
public int QuotaId{get;set;}
public List<quota> Quotas{get;set;}
}
public class Quota
{
public int Id{get;set;}
public string Name{get;set;}
}</quota>
创建一个控制器和两个ActionResults
Create a controller and two ActionResults
public ActionResult Index()
{
var quota=new List<quota>{new Quota{Id=1,Name="Tatkal"},new Quota{Id=2,Name="Ladies"}};
return View(quota);
}
</quota>
创建一个动作以获取下面的下拉列表值
Create a action for get the value of dropdownlist like below
public ActionResult Create(QuotaViewModel quotaViewModel)
{
//you get the selected id while clicking the button
int selectedId=quotaViewModel.QuotaId
return View();
}
创建一个应返回模型的视图QuotaViewModel
Create a view which should return the model QuotaViewModel
@model QuotaViewModel
@{
}
@using(Html.BeginForm("YourControllerName","Create", FormMethod.Post)){
@Html.DropDownListFor(m => m.QuotaId, Model.Quotas)
<input type="submit" value="click here"/>
}
希望这有帮助
Hope this helps
这篇关于在MVC3中获取选定的下拉列表框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文