开发者

Aliasing Querystring Params in BeginForm with FormMethod.Get - MVC2

开发者 https://www.devze.com 2023-01-17 05:24 出处:网络
The Question Is there a way to alias the parameters generated by MVC, in my case it would be the searchTerm param?For example, in my viewmodel it would still be SearchViewModel.searchTerm but the URL

The Question

Is there a way to alias the parameters generated by MVC, in my case it would be the searchTerm param? For example, in my viewmodel it would still be SearchViewModel.searchTerm but the URL would look more like /Home/Search?s=jimmy

Search Form

<% using (Html.BeginForm("Search", "Home", FormMethod.Get)) {%>
       <%: Html.TextBoxFor(model => model.searchTerm) %>
       <input type="submit" value="Search" />
<% } %>

View Model

    public class SearchViewModel
    {
        public string searchTerm { get; set; }
    }

The URL Generated

/Home/Search?searchTerm=jimmy
开发者_开发技巧

Edit: Doesn't seem possible. Looks like I'll have to do it on my own..


I think you could either change your textbox control to not use templating and use the name your want to see in the query string like this...

<%= Html.TextBox("s", Model.searchTerm) 

Or you could change the template that the TextBoxFor method uses with a own custom one, but I'm not sure what convention you would use. Maybe check for modelnames of "searchTerm"?

This site has some examples of how to create a custom template.

Sorry, if the syntax was not correct in the code, but I do not have MVC on this machine.

0

精彩评论

暂无评论...
验证码 换一张
取 消