Reputation: 33
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
Upvotes: 1
Views: 5513
Reputation: 671
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.
Upvotes: 0
Reputation: 1
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" })
Upvotes: 0
Reputation: 2973
Try this:
@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)
instead of this:
@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")
Upvotes: 0
Reputation: 11203
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)
Upvotes: 8
Reputation: 643
Try using Html.Partial(...)
instead of Html.RenderAction(...)
to render the partial view. This may be suppressing the validations.
Upvotes: 0