So I have a controller that derives from SurfaceController.
I have a partial view.
When the user hits submit, it posts to the controller method. This method will do some validation on the model and other business logic.
If it fails, I want to return the model back to the partial view showing the error message. I use in this case ModelState.AddModelError to add the error to the model state.
Now, I tried this but what happens is that the action seems to be called again! And I don't see the error at all on the partialview area.
Any ideas why? I don't understand why when doing:
return PartialViewResult(model);
invokes the action again?
here is the code I am using...
[HttpPost]
public PartialViewResult Login(UserLogin ReceivedModel) { if (ModelState.IsValid == false) { return PartialView(ReceivedModel); }
try {
var returnedData = someClass.DoSomething(ReceivedModel);
Try return CurrentUmbracoPage(); instead of where you are using return new PartialViewResult(). What you are doing is correct in "standard" MVC but Umbraco has these methods available in surface controllers to do what you need but respect the Umbraco routing.
In the success instance - where you don't want to return to the view - use RedirectToCurrentUmbracoPage() or RedirectToUmbracoPage(pageId);
Returning back to the same partial view?
Hi. I am new to Umbraco so bare with me.
Using v6.1.x and also using MVC.
So I have a controller that derives from SurfaceController.
I have a partial view.
When the user hits submit, it posts to the controller method. This method will do some validation on the model and other business logic.
If it fails, I want to return the model back to the partial view showing the error message. I use in this case ModelState.AddModelError to add the error to the model state.
Now, I tried this but what happens is that the action seems to be called again! And I don't see the error at all on the partialview area.
Any ideas why? I don't understand why when doing:
return PartialViewResult(model);
invokes the action again?
here is the code I am using...
[HttpPost]
public PartialViewResult Login(UserLogin ReceivedModel)
{
if (ModelState.IsValid == false)
{
return PartialView(ReceivedModel);
}
try
{
var returnedData = someClass.DoSomething(ReceivedModel);
if (!returnedData.Valid)
{
ModelState.AddModelError("Problem....");
return PartialView(ReceivedModel);
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex);
return PartialView(ReceivedModel);
}
}
So when I do return PartialView(ReceivedModel), it invokes this same action again then displays the page but does not show the error.
Hey Ahmed,
Have you seen this: http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx ?
Try return CurrentUmbracoPage(); instead of where you are using return new PartialViewResult(). What you are doing is correct in "standard" MVC but Umbraco has these methods available in surface controllers to do what you need but respect the Umbraco routing.
In the success instance - where you don't want to return to the view - use RedirectToCurrentUmbracoPage() or RedirectToUmbracoPage(pageId);
There's s good example in the docs here.
Hope that helps.
Andy
is working on a reply...