Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ahmed Ilyas 7 posts 27 karma points
    Jan 29, 2014 @ 22:00
    Ahmed Ilyas
    0

    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.

     

  • Zakhar 171 posts 397 karma points
    Jan 30, 2014 @ 12:41
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 30, 2014 @ 13:38
    Andy Butland
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft