CodeBon
CodeBon

Reputation: 1154

C# ASP NET MVC 5.0 Postback goes to Html.RenderPartial() on View Model Validation Error

I have an unexpected post back to a Html.RenderPartial() action when the main page fails model data validation. If there are no errors with model validation the page posts back to the expected controller action. Has anyone run into this and what solution did they use?

Further explanation:

I have a MVC .cshtml view which has a post back setup as follows:

@using (Html.BeginForm("Profile", "User", FormMethod.Post))
{
    // html code and stuff..

    @{ Html.RenderAction("ImageUpload", "User"); }
    <input type="submit" value="Save" />
}

When I click the "Save" button the page should post back to User/Profile as setup in the Html.BeginForm method: @using (Html.BeginForm("Profile", "User", FormMethod.Post)).

The page works as expected when I do a post back and model validation has no errors...it posts to User/Profile as expected.

However, when there is a validation error with the main view model the page posts back to the Html.RederPartial action: @{ Html.RenderAction("ImageUpload", "User"); }. In other words, instead of posting to User/Profile as expected it posts to User/ImageUpload.

SNAPSHOT OF VIEWMODEL (model provided to User/Profile)

[Required]
[StringLength(1000, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 20)]
[DataType(DataType.Text)]
[Display(Name = "About Me* (Write up to 1000 characters about yourself)")]
public virtual string Summary { get; set; }

public ActionResult Profile(UserModel userModel)
{
    if (ModelState.IsValid)
    {
      //DO STUFF
    }

    // If we got this far, something failed, redisplay form
    return View(userModel);
}

Where the user does not meet the model validation by entering less than 20 characters in the Summary property (the min length validation parameter) the post back goes to the Html.RenderPartial() rather than the expected Html.BeginForm("Profile", "User", FormMethod.Post).

Has anyone run into this before and any ideas on how to solve?

Upvotes: 0

Views: 276

Answers (0)

Related Questions