Parminder
Parminder

Reputation: 3158

FormMethod.Get and query string parameters

I am working on a website in asp.net mvc. I have to show a view where user put some search values like tags and titles to search. I want to use the same Index method for that. I have make my form to use formMethod.Get to send the parameters as querystring.

so here is the method

    [HttpGet]
    public ActionResult Index(string title, string tags, int? page)
    {
        if (string.IsNullOrEmpty(title)
           return View(null);

        var list = GetSomeData();

         return View(list);

    } 

here is my view

<div id="searchBox">
                <% using (Html.BeginForm(null, null, FormMethod.Get))
                   { %>
                <table>
                    <tr>
                        <td>
                            <input type="hidden" id="isPosted" name="isPosted" value="1" />
                                I am looking for
                                <%=Html.TextBox("Title")%>
                                Tags:
                                <%=Html.TextBox("Tags")%>
                                <input id="search" type="submit" value="Search" />
                            </td>
                    </tr>
                </table>
                <% } %>

So when the user first visit the page, he will see only two text boxs and a button. but when he types something in the title and tags and click the search button i will load the view with some data.

Now the problem is when i type something in title and tags box and click search, they are received in the method, but are not visible in the url. Is there anything i m doing wrong.

help will be appreciated.

Regards

Parminder

Upvotes: 1

Views: 3685

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

I would recommend you splitting in two controller actions:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(string title, string tags, int? page)
{
    var list = GetSomeData(title, tags, page);
    return View(list);
}

In this case you definitely no longer need the hidden field inside your form:

<% using (Html.BeginForm()) { %>
    I am looking for
    <%= Html.TextBox("Title") %>

    Tags:
    <%= Html.TextBox("Tags") %>

    <input id="search" type="submit" value="Search" />
<% } %>

Upvotes: 2

Raj Kaimal
Raj Kaimal

Reputation: 8304

I don't understand the part about "Now the problem is when i type something in title and tags box and click search, they are received in the method, but are not visible in the url."

The data is sent either using POST or GET. You have specified GET and I don't see any redirections or AJAX calls so I'm not sure why you don't see it in the URL.

--

Try the sample code at the link below. Hopefully that helps.

http://weblogs.asp.net/rajbk/archive/2010/05/08/asp-net-mvc-paging-sorting-filtering-using-the-mvccontrib-grid-and-pager.aspx

Upvotes: 0

griegs
griegs

Reputation: 22760

This isn't striclty an answer to your specific question more of a recommendation on what I would do instead.

For this type of functionality I'd be using jQuery to do a partial post back with the entered data. I'd then be returning a partial view back to the view and rendering it.

Sounds complex but it isn't. What this then gives you is a smooth search function where the whole page is never posted and so it looks slicker.

The jQuery to do a post looks like this;

$.post("/articles/getFilteredTagList", { filter: tagString }, function(newHTML) {
  document.getElementById("tagList").innerHTML = newHTML;
});

newHTML will contain the rendered html that represents the partial view.

In your controller you then have something like;

public ActionResult getFilteredTagList(string filter)
{
  return PartialView("TagList",new Repository.KeywordsRepository().All().OrderBy(k => k.keyword1));
}

In the code above you are basically instructing mvc to render a partial view and give back the html. this html is what you return to your view again and you can then replace the insides of a div with the new html.

Your partial view may contain other controls that now become visible with data already loaded.

Upvotes: 0

Related Questions