My asp.net mvc 3 project has a search by tag, I used ajax to send the tag st ring to the controller, and perform the search, then return or redirtect to the result page, but the return View(galleries), or return RedirectToAction("SearchResults"); not working.
ajax call triggered properly and hit the controller action, search has been done with repository pattern, but when returned to the controller, it did not go anywhere, just stay on the same search page.
Maybe it is the problem of the link which sends ajax call on click: <a class="search" id="@item.tagId" href="">@item.tagName</a> , maybe I should use someting like @Url.content (...)?
You can't issue a redirect from the search action because that is another request and can't manipulate the original page. The ajax request is it's own request and while you can redirect it, it will redirect the ajax request, not the browser page (the intial
request).
Looking at your code though, you don't want to perform a redirect, instead you should be returning the search results view (make sure it's a partial with no layout information). You can then insert that into a div on your page.
//The controller
public ActionResult Search(string searchString, int objId){
IEnumerable<GalleryUIModel> galleries= helperRepository.GetGalleryByTagName(searchString).Distinct();
return View("SearchResults",galleries);
}
//The JQuery code in the view
$.ajax({
type: "POST",
url: "/Search/Search",
data: "searchString=" + query + "&objId=1"
success : function(data){
$("#results").html(data); // assuming <div id="results"></div> exists on your page
}
});
You can do either, if you choose to use jquery ui dialog, you can just put the results in the div as before, then just simply launch the dialog.
If you want to forward to another page, then instead of an ajax call, just make the search form a normal form post. With an ajax call and redirect to another page, you're adding extra complexity and two requests, when only one is needed.
byuan
Member
138 Points
124 Posts
ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 08:47 PM|LINK
My asp.net mvc 3 project has a search by tag, I used ajax to send the tag st ring to the controller, and perform the search, then return or redirtect to the result page, but the return View(galleries), or return RedirectToAction("SearchResults"); not working.
Code:
On the search page:
<ul>
@foreach (var item in Model.allTags)
{
<li style="list-style:none; display:inline">
<a class="search" id="@item.tagId" href="">@item.tagName</a>
(@item.tagCount)
<script type="text/javascript">
$(document).ready(function () {
$("#@item.tagId").click(function () {
var query = $("#@item.tagId").text();
$.ajax({
type: "POST",
url: "/Search/Search",
data: "searchString=" + query + "&objId=1"
});
return false;
});
});
</script>
</li>
}
</ul>
Controller:
public ActionResult Search(string searchString, int objId)
{
IEnumerable<GalleryUIModel> galleries;
galleries = helperRepository.GetGalleryByTagName(searchString).Distinct();
return RedirectToAction("SearchResults");
//return View(galleries);
}
I also created a view on the action Search using strongly typed model as follows:
@model IEnumerable<Model.Helpers.PresentationModel.GalleryUIModel>
<table>
<tr>
<th>
galleryId
</th>
......
</table>
ajax call triggered properly and hit the controller action, search has been done with repository pattern, but when returned to the controller, it did not go anywhere, just stay on the same search page.
Maybe it is the problem of the link which sends ajax call on click: <a class="search" id="@item.tagId" href="">@item.tagName</a> , maybe I should use someting like @Url.content (...)?
Please help. Thanks in advance.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 09:00 PM|LINK
You can't issue a redirect from the search action because that is another request and can't manipulate the original page. The ajax request is it's own request and while you can redirect it, it will redirect the ajax request, not the browser page (the intial request).
Looking at your code though, you don't want to perform a redirect, instead you should be returning the search results view (make sure it's a partial with no layout information). You can then insert that into a div on your page.
//The controller public ActionResult Search(string searchString, int objId){ IEnumerable<GalleryUIModel> galleries= helperRepository.GetGalleryByTagName(searchString).Distinct(); return View("SearchResults",galleries); } //The JQuery code in the view $.ajax({ type: "POST", url: "/Search/Search", data: "searchString=" + query + "&objId=1" success : function(data){ $("#results").html(data); // assuming <div id="results"></div> exists on your page } });Blog | Twitter : @Hattan
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 09:13 PM|LINK
Ok, thanks for your help, let me try that to see how that works.
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:11 PM|LINK
How about I need to return search result view to another page, instead of embedding the result in the current page, make any sense?
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:20 PM|LINK
Or use Jquiery UI dialog to show the search results?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:21 PM|LINK
You can do either, if you choose to use jquery ui dialog, you can just put the results in the div as before, then just simply launch the dialog.
If you want to forward to another page, then instead of an ajax call, just make the search form a normal form post. With an ajax call and redirect to another page, you're adding extra complexity and two requests, when only one is needed.
Blog | Twitter : @Hattan
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:28 PM|LINK
Thank you very much for the help.
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:34 PM|LINK
One more question, if I decisded to use jquery dialog, where would I put the code to popu the dialog, in the success function of the ajax call?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:43 PM|LINK
Yes, in the success because you only want the dialog to appear after the results come back from the server.
Blog | Twitter : @Hattan
byuan
Member
138 Points
124 Posts
Re: ASP.NET MVC Razor Return View() Not Work
Apr 13, 2012 11:45 PM|LINK
Thanks.