Reputation: 864
Im new to MVC.. and started to grasp it. I am wondering whether there's a shorter way to pass parameters from view to mvc controller. I am creating a search box with possible conditions.
here's the sample code in the view
@using (Html.BeginForm("Action", "Controller", FormMethod.Get))
{
<p>
Find by fname: @Html.TextBox("fname", ViewBag.CurrentFilter as string)
<br/>
Find by lname: @Html.TextBox("lname", ViewBag.CurrentFilter as string)
<br/>
By Area Code : @Html.TextBox("AreaCode")
<br/>
@Html.DropDownList("StateCD", new SelectList(ViewBag.State))
<br/>
<input id="Button1" type="submit" value="Search"/>
</p>
}
in my controller
public async Task<ActionResult> Action(string id, string sortorder, string statecd,int? page,string country,string areacode,string city,string zip,string z)
{
}
is there a way to shorten that parameter like as an object or concatenated values?
Upvotes: 0
Views: 1753
Reputation: 218722
You can create a class to represent your search filter and use that as the action method parameter type.
public class SearchVm
{
public strng StateCd { set;get;}
public string AreaCode { set;get;}
public string Zip { set;get;}
// Add other needed properties as well
}
and use that as the param
public async Task<ActionResult> Search(SearchVm model)
{
// check model.AreaCode value etc..
// to do : Return something
}
When the form is submitted Default Model binder will be able to map the posted form data to properties of the method parameter object, assuming your form field name's match with the class property names
Upvotes: 0