Passing a dropdown selected value from a view to a controller in mvc3?

I have a mvc3 web application. In that I used EF and populated two dropdownlists from the database.

Now, when I select the values ​​from these dropdownlists, I need to show them inside the 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 the button Mapping, it will go to a new page called Mapping.cshtmland should show a webgrid with these two values.

+3
source share
2 answers

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

}

HttpPost ViewModel ,

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

mapping actionresult. actionresult mapping(string ID, string ddID). ViewData. viewmodel

0

All Articles