ASP.NET MVC 3 unobtrusive client-side validation with dynamic content

前端 未结 2 1198
星月不相逢
星月不相逢 2021-01-22 14:45

I have enabled unobtrusive client side validation in my app, and I have (something similar) to this model:

public class MyModel
{
    [Required]
    public strin         


        
2条回答
  •  长情又很酷
    2021-01-22 14:58

    Ahh... simple :)

    The @Html.BeginForm() must be in the same partial (which I guess is fair enough, since we are validating on a form), so:

    /Home/Index view becomes:

    @model MyModel
    
    
    @Html.Partial("Model")

    With the partial "Model":

    @model MyModel
    
    @using (Html.BeginForm("Test", "Home"))
    {
        
    @Html.LabelFor(x => x.Name) @Html.TextBoxFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name)
    @Html.LabelFor(x => x.Age) @Html.TextBoxFor(x => x.Age) @Html.ValidationMessageFor(x => x.Age)
    }

提交回复
热议问题