1

Hi how are you? I'm trying to validate a form in ASP NET MVC.

I have a partial view "Address" that I reuse for some entities, like Company, Person, etc.

My problem is that when I submit the form, only the controls of the father view gets validated, the ones in the partial view don't.

Here's some code I hope u can geive me hand

PERSON VIEW

@model Entities.Person


@using (Html.BeginForm("Create", "Person", FormMethod.Post))
{

    <table>
        <tr>
            <td>
                @Html.LabelFor(model => model.FirstName)
                <div class="control">
                    @Html.TextBoxFor(model => model.FirstName, new { @maxlength = 7, @class = "numeric"})
                    @Html.ValidationMessageFor(model => model.FirstName)
                </div>
                <div class="spacer-short"></div>
            </td>
            <td>
                @Html.LabelFor(model => model.LastName)
                <div class="control">
                    @Html.TextBoxFor(model => model.LastName, new { @maxlength = 7, @class = "numeric"})
                    @Html.ValidationMessageFor(model => model.LastName)
                </div>
                <div class="spacer-short"></div>
            </td>
        </tr>
    </table>
    @{ Html.RenderAction("Index", "Address", new {id = Model.AddressId});} //Renders the Address form part

    <div class="spacer"></div>
    <button type="submit" data-button="true" data-button-icon="ui-icon-disk" class="">Create</button>
    <button type="button" data-button="true" data-button-icon="ui-icon-circle-close" class="button-cancel">Cancel</button>
}

ADDRESS VIEW

@model Entities.Address


<table>
     <tr>
         <td>
            @Html.LabelFor(model => model.Street)
            <div class="control">
                @Html.TextBox("Address.Street", Model.Street)
                @Html.ValidationMessage("Address.Street")
             </div>
           <div class="spacer-short"></div>
           </td>
           <td>
              @Html.LabelFor(model => model.Number)
              <div class="control">
                @Html.TextBox("Address.Number", Model.Number)
                @Html.ValidationMessage("Address.Number")
             </div>
           <div class="spacer-short"></div>
            </td>
        </tr>
    </table>

Then I have some validation metadata ([Required]) for person and address fields

1
  • Have you tried replacing @{Html.RenderAction...} with @Html.Partial("Address") Commented Mar 18, 2013 at 17:18

5 Answers 5

8

A common mistake using @Html.Partial(...) and expecting validation errors to show up is not passing ViewData to that partial.:

@Html.Partial([ViewName], [EmailAddressObject], ViewData)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that helped. The catch is that when you only call partial with two first argument (viewname and usually model) it works fine (so I guess ViewData gets passed implicitly). But if you pass something custom (for example a flag to change output of the partial) and don't attach existing ViewData, you will lose some information. In my case the input-validation-error class wasn't applied to the inputs with validation error contained in partial.
0

Try this:

@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)

instead of this:

@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")

Comments

0

Try using Html.Partial(...) instead of Html.RenderAction(...) to render the partial view. This may be suppressing the validations.

1 Comment

I've tried Html.RenderPartial before using RenderAction with the same results. That would be the same as Html.Partial doesn't it?
0

Model

İmportant: Manage Nuget Packages

=>> JQuery.Validation

=>> Microsoft.JQueryUnobtrusive.Validation

install.

1) First step

using System.ComponentModel.DataAnnotations;

2)

    public int Id { get; set; }
    [Required(ErrorMessage = "* Kategori adı boş geçilemez.")]
    [DisplayName("Kategori Adı")]
    public string CategoryName { get; set; }
    [Required(ErrorMessage = "* Kategori açıklaması boş geçilemez.")]
    [DisplayName("Kategori Açıklaması")]
    public string Description { get; set; }

3)

if (model != null && ModelState.IsValid)
                {
                    var categoryCreate = new Categories
                    {
                       //code
                    };
                    _CategoriesService.Create(categoryCreate);
                }

4) Add ViewModel @model Models.CategoryViewModel After

@Html.TextBoxFor(model => model.CategoryName, new { @class = "form-control input-sm", id = "categoryTitle", @placeholder = "Kategori adını giriniz.", @type= "text", @required=true })

@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "error-message" })

@Html.TextBoxFor(model => model.Description, new { @class = "form-control input-sm", id = "categoryArticle", @placeholder = "Kategori açıklamasını giriniz.", @type = "text", @required = true })

@Html.ValidationMessageFor(model => model.Description, "", new { @class = "error-message" })

Comments

0

In my case, I was missing

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

Once I put that in my _layout.cshtml file, it started working even with partial views.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.