I am trying to redirect to action and get a new view (a new page) with no success. While debugging, I'm reaching the controller but not getting the view (the page URL is not changed).
With Fiddler I see that the page returns the right view result but in the browser the URL is not changed!
When shopping cart is empty, I would like to redirect to a new page a display the error message.
[HttpPost]
public ActionResult PlaceOrder(DeliveryDetails deliveryDetails)
{
if (UserCart.IsEmpty)
{
TempData["errorMsg"] = "Error: Cart is empty";
return RedirectToAction("Index", "Error");
}
else
{
// do something..
}
}
ErrorController:
public ActionResult Index()
{
return View();
}
ErrorController View:
#TempData["errorMsg"]
Any suggestions on what is going on ?
It was a js problem:
event.isDefaultPrevented()
Is your 'ErrorController View:' should be named Index.cshtml? If it is not it should be.
Related
Recently I've created on controller call DashboardVideos and an action method called Index.
And after Add Or Update, I'm redirecting it to Index page using
RedirectToAction("Index", "DashboardVideos").
but this code redirecting it to /DashboardVideos/ and it says
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
so the issue is by default it's supposed to load Index page when I say /Dashboard
But its not, same url pattern working with all other controller (So I don't think there's anything wrong with routing pattern).
Any help would be appreciated.
Code:
public class DashboardVideosController : BaseController
{
private readonly IDashboardVideosComponent socialTagComponent;
public DashboardVideosController()
{
socialTagComponent = ComponentFactory.Get<IDashboardVideosComponent>();
}
// GET: DashboardVideos
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index", "DashboardVideos");
}
}
Simply write this if both actions are in same controller.
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index");
}
Try to take a look at your "RouteConfig" class and you can specify custom routes there. Also It is possible if the call comes from AJAX it can go directly to action without redirecting. Did you tried to Debug the code?
I am trying to redirect to action and get a new view (a new page) with no success. While debugging, I'm reaching the controller but not getting the view (the page URL is not changed).
With Fiddler I see that the page returns the right view result but in the browser the URL is not changed!
When shopping cart is empty, I would like to redirect to a new page a display the error message.
[HttpPost]
public RedirectToRouteResult PlaceOrder(DeliveryDetails deliveryDetails)
{
if (UserCart.IsEmpty)
{
TempData["errorMsg"] = "Error: Cart is empty";
return RedirectToAction("Index", "Error");
}
else
{
InsertOrder();
}
}
ErrorController:
public ActionResult Index()
{
return View();
}
ErrorController View:
#TempData["errorMsg"]
Thanks.
Then I would surmise that UserCart.IsEmpty is evaluating to false. What does your Error Index route look like? Also, you're better off returning a base ActionResult from a controller action in case you need to return a view. Presumably there's more code in the PlaceOrder method because that won't compile as it stands
Use ActionResult instead of RedirectToRouteResult
[HttpPost]
public ActionResult PlaceOrder(DeliveryDetails deliveryDetails)
{
// .....
return RedirectToAction("Index", "Error");
}
I've noticed that when the view is returned because of validation errors , the url still points to the action that has done the validation :
for example in Edit View there is a form to Update action:
#Html.BeginForm("Update","MyController",FormMethod.Post,new{})
{
...
}
Update action :
public ActionResult Update(Entity myEntity)
{
is(ModelState.IsValid)
{
...
return RedirectToAction("List");
}
return View("Edit",myEntity);
}
when validation fails this shows the Edit view again with errors but the address is
.../Update
am I doing something wrong ? how to fix this ?
am I doing something wrong ? how to fix this ?
That's how it works, you can try RedirectToAction with TempData for model.
Like here:
ASP.NET MVC ActionResult View() not changing url
What you would want to do is instead of posting it to "Update", post to the same action you are on, but mark with [HttpPost] like the following
[HttpPost]
public ActionResult Edit(SomeViewModel someViewModel) {
is(ModelState.IsValid)
{
...
return RedirectToAction("List");
}
return View(myEntity);
}
Methods with HttpPost will only get invoked when the browser detects "Post" Request. When user enters url through browser, it's a "Get" request and so the other "Edit" Action will get invoked.
New to MVC and having trouble trying to "Logout" of my site. I have a LogOn() method and view that comes up first to show the Log On page. After I log on then I go to a another page with a button to log out. (note: this is not likely directly related to logging in/out. that is just the story I'm working on)
<asp:Content runat="server" ContentPlaceHolderID="GlobalNavbarPrimary" ID="Content3">
Logout
</asp:Content>
When I press the button then it correctly finds my SignOut method. However, after I logout I try to run a redirect to go back to the LogOn action. I'm not understanding why my LogOn method is not run/found. Here are my LogOn methods and my SignOut method.
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
string sourceIp = string.Empty;
if (!string.IsNullOrEmpty(Request.UserHostAddress))
sourceIp = Request.UserHostAddress;
try
{
XX.User user = new XX.User();
XX.SessionKey sk = null;
sk = user.Logon(model.EmailAddress, model.Password, sourceIp);
Session[MyAuthorizeAttribute.MySessionKeyName] = sk.Value;
return RedirectToAction("Index", "Message");
}
catch (Exception ex)
{
ModelState.AddModelError(ex.Message, "Fatal Error");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public ActionResult SignOut()
{
CM.User user = new CM.User(Key);
user.Logout(Key);
// This does not go to the LogOn action!!!!!
return RedirectToAction("LogOn", "Account");
What is also troubling is that the address bar shows http://localhost:1159/#/Account/SignOut, whereas I was expecting to see http://localhost:1159/#/Account/LogOn (or ....//localhost:1159/)
Any insight (or suggestions for debugging strategies) would be most welcome.
I found that if I removed jQuery-mobile from my master page then it works as expected. I had seen some strange things, such as the source of the page (View Source) not matching what I was looking at (actually showing the source of a different page) - an impossibility, so I thought.
I'll have to look at jQuery-mobile closer. I know it is in beta, so perhaps I just hit a known issue.
public ActionResult Index()
{
var tickets =
DependencyContainer.DomainController.ShowTickets();
if(tickets==null)
{
ViewData.Add("Error","Associate not found");
return RedirectToAction("Index", "Home");//Doesn't fire the index?
}
return View();
Home controller method is as follows and only fires on the debugger startup, and after the redirect IF I hit F5.
public ActionResult Index()
{
ViewData["Message"] = "Welcome to Home Loans and Insuring Technology Ticketing";
//if (TempData.ContainsKey(ErrorKey))
//{
// ViewData[ErrorKey] = TempData[ErrorKey];
// TempData.Remove(ErrorKey);
//}
return View();
}
Redirect doesn't fire the controller?
You must use TempData when redirecting. ViewData will not work when set before a redirect.
Do you cache the site index? Does the browser cache it? Look at the request and response with Firebug or Fiddler.