c# - MVC BeginForm - passing parameters to controller using GET -


i implementing search filter 1 of application's views. struggle @ getting routevalues passed controller action using @html.beginform() , request.

the action accepts following properties:

public actionresult books(int id, string type, string search) {     //rest of code } 

the view's search box looks this:

@model ilookup<string, citylibrary.models.library.book> ....  @using (html.beginform("books", "collections", new { id = model.first().first().collectionid, type = viewbag.booktype }, formmethod.get, null)) {     <div class="input-group col-md-4">         @html.textbox("search", null, new { @class = "form-control form-control-fixed-width", @placeholder = "filter title..." })         <span class="input-group-btn">             <button class="btn btn-default" type="submit">                 <span class="glyphicon glyphicon-search"></span>             </button>         </span>     </div> } 

the problem occurs when submitting search box. controller action gets id , search string, type null, though viewbag.booktype not null. fiddler shows this:

get /collections/books/2?search=searchterm

which seems ignoring type parameter in request.

source code in browser:

<form action="/collections/books/2?type=available" method="get">     <div class="input-group col-md-4">         <input class="form-control form-control-fixed-width" id="search" name="search" placeholder="filter title..." type="text" value="" />         <span class="input-group-btn">             <button class="btn btn-default" type="submit">                <span class="glyphicon glyphicon-search"></span>             </button>         </span>     </div> </form>  

does have method? avoid posting have write controller action same code.

edit: seems problem occurs when try use request. posting form passes parameters controller action. why that?

this behavior in accordance html specifications, in particular form method="get", (my emphasis)

mutate action url

let destination new url equal action except <query> component replaced query (adding u+003f question mark character (?) if appropriate).

so query string value in form's action attribute replaced query string generated name/value pairs of form controls.

two options solve this:

  1. remove new { type = viewbag.booktype } beginform() method , add hidden input parameter

    <input type="hidden" name="type" value="@viewbag.booktype" /> 
  2. create custom route definition method type added route parameter, not query string value (note must before default route)

    routes.maproute(     name: "books",     url: "collections/books/{id}/{type}",     defaults: new { controller = "collections", action = "books" } ); 

    so current beginform() code generate

    <form action="/collections/books/2/available" method="get"> 

and form submit result in url of collections/books/2/available?search=searchterm


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -