0

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?

2
  • stackoverflow.com/questions/17558025/… etc. try searching around more Commented Sep 1, 2016 at 14:28
  • Create a View Model and use it to pass data as parameter to controller. Commented Sep 1, 2016 at 14:28

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.