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 30, 2014 @ 14:40
    Ahmed Ilyas
    0

    Posting back and displaying view again if model validation fails?

    So, I am new to Umbraco and taking on an existing project.

    I am using MVC4 and umbraco 6.1.1

    So, I have a model and a controller.

    The controller inherits from SurfaceController.

    I have a partial view that uses the model.

    Now when the user posts back to the controller, and if the model validation fails, I want to return back to that view (with the model like you would do in normal MVC) and display the error included in the ModelState.

    unfortunately, the page doesnt seem to show the errors even though I added it. It still contains the existing form details but no error messages are shown?

     

    Code:

     

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Login(UserLogin ReceivedModel)
            {
                if (ModelState.IsValid == false)
                {
                    return PartialView(ReceivedModel);
                }

                try
                {
                  // blah blah
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex);
                    return CurrentUmbracoPage();
                }
    }

    now, this does catch the Exception, adds the error to the modelstate and returns the current umbraco page but as I explained, it does not show the error.
    any ideas?

     

    Here is the View:

    @model Models.UserLogin


    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(false, string.Empty, new { @class = "error" })
       
    // BLAH BLAH BLAH

     

     <input type="submit" value="Submit" />

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 30, 2014 @ 18:37
    Ismail Mayat
    0

    Not exactly solution but see See http://our.umbraco.org/forum/using/ui-questions/44136-Umbraco-6-MVC-form-action-advice there is link to issue on issue tracker http://issues.umbraco.org/issue/U4-1339 i also had this issue.

    Regards

    Ismail

Please Sign in or register to post replies

Write your reply to:

Draft