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

时间:2012-04-12 07:30:19

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

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

现在当我从那些下拉列表中选择值时,我需要在webgrid中显示它们 我怎么能这样做?

@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显示这两个值。

2 个答案:

答案 0 :(得分:3)

我会创建一个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...   
}

答案 1 :(得分:0)

将表单发布到mapping actionresult。在actionresult映射中,接收参数下拉为mapping(string ID, string ddID)。使用ViewData查看这些值。 一种更好的方法是为网格视图创建一个视图模型,并使您的映射视图具有强类型,并根据需要在网格上使用值

相关问题