I have a small search form on a page in my application. This search form will - on successful search - redirect you to another view where it lists your results. If the search word could not be found (there are specifics) I want to display a message "Couldn't
find what you were looking for" or similar inside the same form on page nr 1.
How do i return to my form in my view when the controller realises the search word does not exist?
public class QuickSearchModel {
public string SearchWord{ get; set; }
public string ErrorMessage { get; set; }
}
public virtual ActionResult QuickSearch(string searched)
{
// Query to retrieve search results
if (result == null) {
QuickSearchModel model = new QuickBookingModel() {
SearchedWord = searched;
ErrorMessage = "Couldn't find " + searched + ".";
};
// This i the return that im concerned about
return PartialView("~/...", model);
}
else
return Redirect("~/..."); // This will go to the search result page, this works fine
}
As you can see in my controller i am returning the result page if i have something to show, otherwise im going to a partial view. This partial view does not care about the view that it's in. How can i write so that it goes back into the pocket in the view
that it was previously in? Just, redirect back with the error message in the model.
add 2 parameters to your action: the controller name and the action name. Use TempData to pass your ErrorMessage. RedirectToAction(controller/action parameters)
It doesn't actually have an action - the partial view which has the search form.
If i were to create an action for it, how can i specify where this partial should be located? (inside my view nr 1)
Edit
Oooh - i should redirect to the view page with.. i see.. i get it :D
Edit again
But i get error messages when the TempData isnt being used. "!string.IsNullOrEmpty(TempData["errormessage"].ToString()) ? TempData["errormessage"].ToString() : "" " Can i do it in another way?
Jennica
Member
500 Points
341 Posts
Back and forth
Jun 14, 2011 07:03 AM|LINK
Hello,
I have a small search form on a page in my application. This search form will - on successful search - redirect you to another view where it lists your results. If the search word could not be found (there are specifics) I want to display a message "Couldn't find what you were looking for" or similar inside the same form on page nr 1.
How do i return to my form in my view when the controller realises the search word does not exist?
public class QuickSearchModel { public string SearchWord{ get; set; } public string ErrorMessage { get; set; } }public virtual ActionResult QuickSearch(string searched) { // Query to retrieve search results if (result == null) { QuickSearchModel model = new QuickBookingModel() { SearchedWord = searched; ErrorMessage = "Couldn't find " + searched + "."; }; // This i the return that im concerned about return PartialView("~/...", model); } else return Redirect("~/..."); // This will go to the search result page, this works fine }As you can see in my controller i am returning the result page if i have something to show, otherwise im going to a partial view. This partial view does not care about the view that it's in. How can i write so that it goes back into the pocket in the view that it was previously in? Just, redirect back with the error message in the model.
Any ideas?
ignatandrei
All-Star
134838 Points
21603 Posts
Moderator
MVP
Re: Back and forth
Jun 14, 2011 07:27 AM|LINK
Jennica
Member
500 Points
341 Posts
Re: Back and forth
Jun 14, 2011 07:33 AM|LINK
It doesn't actually have an action - the partial view which has the search form.
If i were to create an action for it, how can i specify where this partial should be located? (inside my view nr 1)
Edit
Oooh - i should redirect to the view page with.. i see.. i get it :D
Edit again
But i get error messages when the TempData isnt being used. "!string.IsNullOrEmpty(TempData["errormessage"].ToString()) ? TempData["errormessage"].ToString() : "" " Can i do it in another way?
Im not getting it.. :/
ignatandrei
All-Star
134838 Points
21603 Posts
Moderator
MVP
Re: Back and forth
Jun 14, 2011 10:17 AM|LINK
if(TempData.Containskey("errormessage"))