2012-04-12 47 views
3

我MVC3 Web应用程序。 ,在我所使用的EF和填充从数据库中的两个dropdownlists。使从视图下拉列表的选定值在MVC3到控制器?

现在,当我选择的,我需要向他们展示里面的WebGrid 我怎么能做到这一点dropdownlists值?

@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新的一页,必须表现出的WebGrid与这两个值。

回答

3

我想创建一个视图模型

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

}

和在我的获取动作的方法,我将这个视图模型回到我的强类型查看

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行动的方法,我会接受这个视图模型的参数,我将不得不选择的值有

[HttpPost] 
public ActionResult GetThat(YourClassViewModel objVM) 
{ 
    // You can access like objVM.SelectedSecurityId 
    //Save or whatever you do please... 
} 
+0

美丽@Shyju。我们如何在GetThat动作中使用AutoMapper将Domain模型与View模型进行映射? – 2013-07-10 07:45:52

0

帖子的形式mapping actionresult。在动作结果映射接收下拉参数为mapping(string ID, string ddID)。采用这些值来查看使用ViewData。 更好的方法将是使网格视图中的视图模型,使你的映射视图强类型和对电网利用价值,你需要

相关问题