在 mvc3 中将下拉列表的选定值从视图传递到控制器?

2024-04-05

我有 mvc3 Web 应用程序。 我使用了 EF 并从数据库填充了两个下拉列表。

现在,当我从这些下拉列表中选择值时,我需要在网络网格内显示它们 我怎样才能做到这一点?

@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按钮它将转到名为的新页面Mapping.cshtml并且必须显示带有这两个值的网络网格。


我会创建一个 ViewModel

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; }

}

在我的 Get Action 方法中,我将将此 ViewModel 返回到我的强类型视图

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);  
}

在我看来,这是强类型的,

@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 Action 方法中,我将接受此 ViewModel 作为参数,并且我将在那里获得 Selected 值

[HttpPost]
public ActionResult GetThat(YourClassViewModel objVM)
{
   // You can access like objVM.SelectedSecurityId
   //Save or whatever you do please...   
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 mvc3 中将下拉列表的选定值从视图传递到控制器? 的相关文章

随机推荐