MVC. Cant see view after HttpPost

740 views Asked by At

I have the following code for the search button on my MasterPage:

     <form id="form1" runat="server"> 
        <input type="text" value="Поиск" size="25" class="search-form" id="txt_search" name="txt_search" />
        <button type="submit" name="b_search" id="b_search" value="1" class="but-search"  onclick="javascript:MySaveFunc(txt_search); ">&nbsp</button>  

     <script type="text/javascript">
         function MySaveFunc(rec) {
             var new_name = rec.value;

             $.ajax({
                 url: '/Home/SearchPost',
                 type: "POST",
                 cache: false,
                 data: {
                     txt_name: rec.value 
                 },
                 success: function (data) {

                 },
                 error: function (data) {

                 }
             });


             return false;
         }

</script>
    </form> 

I also have the following code in my Controller:

[HttpPost] 
        public  ActionResult  SearchPost(string txt_name)
        {          
           return RedirectToAction("Search", new { txt_name =txt_name });       

        }


        public ActionResult Search(string txt_name)
        {     
            ......

             return View("Search");
        }

And in global.asa

  routes.MapRoute("Search", "Home/Search/{txt_name}", new { controller = "Home", action = "Search", txt_name = UrlParameter.Optional });
  routes.MapRoute("SearchPost", "Home/SearchPost/{txt_name}", new { controller = "Home", action = "SearchPost", txt_name = UrlParameter.Optional }); 

If i call like this- http://localhost:55419/Home/search/126 - It's works fine. But if I click on the "Search" button , the application does not show the page "Search"!!

First go to the "SearchPost" action, then "Search". Without errors. But the last line is not working:

 return View("Search");

The application does not show view "search", only the previous page. What's wrong ?.

1

There are 1 answers

1
Jason Evans On BEST ANSWER

Try this

[HttpPost]
public  ActionResult  SearchPost(string txt_name)
{          
    return JavaScript(string.Format("window.location = '{0}'", Url.Action("Search", "Controller"));
}

You will have to make sure the url to the Search action method is correct, I don't know what the controller name is.

The theory is that since you have made a client-side ajax request, returning View("Search") won't work in your case. Instead, return a JavaScript action result with a command to redirect the browser to the search page. You can add querystring params to the url, or whatever else you need, to pass to the Search action method.