user1625066
user1625066

Reputation:

Asp.Net MVC4 - Form in partial view

I have the following data model

public class User
{
        [Required(ErrorMessage = "First name required!")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last name required!")]
        public string LastName { get; set; }
}

I have the following view called "UserForm" that uses the above model

@model User
@using (Html.BeginForm("myAction", "MyController"))
{
@Html.TextBoxFor(model => model.FirstName)
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationSummary()
<input type="submit" value="Save" />
}

I have tested the above code and unobtrusive validation and form posts work correctly and without any issues. However, if I include the above view as a partial view in a second view as shown below, then client side unobtrusive validation stops working.

<div id="myPartialView">
<!-- Include form as partial view -->
@{Html.RenderAction("myAction", "MyController");}
</div>

I tried explicitly reloading unobtrusive validation using the following line of javascript

jQuery.validator.unobtrusive.parse('#myPartialView');

Now validation is working but I am unable to submit the form. I used fiddler and I can see that clicking on the submit button is not triggering any activity. Any thoughts on what I may be doing wrong?

Upvotes: 3

Views: 4234

Answers (1)

user1625066
user1625066

Reputation:

For anyone stumbling across this question, I have this issue resolved now. My forms were embedded in a Jquery Modal box and I had an invalid html tag. This was not causing the modal to stop working but was stopping my form from submitting. (I am still not sure why). I found this error using the excellent html validator from w3c

http://validator.w3.org

It should be noted that I still need

jQuery.validator.unobtrusive.parse('#myPartialView');

to get client side validation to work for dynamic content. This is a well known issue which is discussed in more detail in the link posted by @Mate above

Upvotes: 2

Related Questions