passing dropdown's selected value from view to controller in mvc3?

偶尔善良 提交于 2019-12-05 06:08:25

问题


I have mvc3 web application. In that i have used EF and populate two dropdownlists from database.

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

when i clicked on Mapping button it will goes to new page called Mapping.cshtml and have to show webgrid with those two values.


回答1:


I would create a 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; }

}

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" />

}

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



回答2:


Post the form to mapping actionresult. in the actionresult mapping receive dropdown in parameters as mapping(string ID, string ddID). Take these values to view using ViewData. A better approach will be to make a viewmodel for grid view and make your mapping view strongly typed and use value on grid as you required



来源:https://stackoverflow.com/questions/10119286/passing-dropdowns-selected-value-from-view-to-controller-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!