从视图中通过下拉菜单中的选定值在MVC3控制器? [英] passing dropdown's selected value from view to controller in mvc3?
问题描述
我有MVC3 Web应用程序。
在我已经使用EF并从数据库填充两个dropdownlists。
I have mvc3 web application. In that i have used EF and populate two dropdownlists from database.
现在,当我从这些dropdownlists选择值,我需要向他们展示里面的WebGrid
我怎样才能做到这一点?
Now when i select values from those dropdownlists i need to show them inside webgrid how can i do this?
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Mapping</legend>
<div class="editor-label">
@Html.Label("Pricing SecurityID")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ID,
new SelectList(Model.ID, "Value", "Text"),
"-- Select category --"
)
@Html.ValidationMessageFor(model => model.ID)
</div>
<div class="editor-label">
@Html.Label("CUSIP ID")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ddlId,
new SelectList(Model.ddlId, "Value", "Text"),
"-- Select category --"
)
@Html.ValidationMessageFor(model => model.ddlId)
</div>
<p>
<input type="submit" value="Mapping" />
</p>
</fieldset>
}
当我在映射点击
按钮,它会进入所谓的新页 Mapping.cshtml
,并有显示的WebGrid与这两个值。
when i clicked on Mapping
button it will goes to new page called Mapping.cshtml
and have to show webgrid with those two values.
推荐答案
我想创建一个视图模型
public class YourClassViewModel
{
public IEnumerable<SelectListItem> Securities{ get; set; }
public int SelectedSecurityId { get; set; }
public IEnumerable<SelectListItem> CUSIPs{ get; set; }
public int SelectedCUSIPId { get; set; }
}
在我付诸行动的方法,我将在视图模型回到我的强类型视图
and in my Get Action method, I will return this ViewModel to my strongly typed View
public ActionResult GetThat()
{
YourClassViewModel objVM=new YourClassViewModel();
objVm.Securities=GetAllSecurities() // Get all securities from your data layer
objVm.CUSIPs=GetAllCUSIPs() // Get all CUSIPsfrom your data layer
return View(objVm);
}
在我看来,这是强类型,
And In my View Which is strongly typed,
@model YourClassViewModel
@using (Html.BeginForm())
{
Security :
@Html.DropDownListFor(x => x.SelectedSecurityId ,new SelectList(Model.Securities, "Value", "Text"),"Select one") <br/>
CUSP:
@Html.DropDownListFor(x => x.SelectedCUSIPId ,new SelectList(Model.CUSIPs, "Value", "Text"),"Select one") <br/>
<input type="submit" value="Save" />
}
而现在我HttpPost行动的方法,我会接受这个视图模型的参数,我将不得不选择的值有
and now in my HttpPost Action method, I will accept this ViewModel as the parameter and i will have the Selected value there
[HttpPost]
public ActionResult GetThat(YourClassViewModel objVM)
{
// You can access like objVM.SelectedSecurityId
//Save or whatever you do please...
}
这篇关于从视图中通过下拉菜单中的选定值在MVC3控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!