Reputation: 25
want to get dropdownlist selected value in Controller after click on Submit button
View part :-
@Html.DropDownListFor(m => m.ExportFormat, new List<SelectListItem>()
{
new SelectListItem(){Value="PDF", Text = "PDF"},
new SelectListItem(){Value=".CSV", Text = ".CSV"},
new SelectListItem(){Value="Excel", Text = "Excel"}
}, "Select Format", new { @class = "form-control editable" })
<input type="submit" value="Export file" class="btn btn-primary" name="Command" />
Controller Part:-
[AllowAnonymous]
public ActionResult Reports(ReportsModel model,string Command)
{
if(Command=="PDF")
{
}
else if(Command==".CSV")
{
}
else if(Command=="Excel")
{
}
}
Upvotes: 0
Views: 495
Reputation: 1612
Hi If you want to get the value of the dropdownlist control, in the controller action method then you should give name of the dropdownlist control as a parameter in the action method.
example:
[AllowAnonymous]
public ActionResult Reports(ReportsModel model,string ExportFormat)
{
}
Hope in your code , the name of the dropdownlist control is exportformat , kindly check that in view source and give it as a parameter , then you should surely get it.
Thanks
Karthik
Upvotes: 1
Reputation: 247
This is not the way to ask a good question! However you can update your @Html.DropDownListFor();
with following piece of code :
@Html.DropDownListFor(m => m.ExportFormat, new List<SelectListItem>()
{
new SelectListItem(){Value="PDF", Text = "PDF"},
new SelectListItem(){Value=".CSV", Text = ".CSV"},
new SelectListItem(){Value="Excel", Text = "Excel"}
},"Model.ExportFormat", "Select Format", new { @class = "form-control
editable", onchange = "this.form.submit()" })
It will be better if you put controller's action where you are submitting the form on change!
Upvotes: 0