public class Person
{
public Name Name {get; set;}
public string Gender {get; set;}
...
}
public class Name
{
public string First {get; set;}
public string Last {get; set;}
...
}
In my edit View, I have the following:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<label for="First">First:</label>
<div class="detail">
<%= Html.TextBox("Name.First", Model.Name.First)%>
<%= Html.ValidationMessage("First", "*")%>
</div><br />
<label for="Name_Last">Last:</label>
<div class="detail">
<%=Html.TextBox("Name.Last", Model.Name.Last)%>
<%= Html.ValidationMessage("Name.Last", "*")%>
</div><br />
...
Lastly, my validation in my controller looks like this:
public ActionResult Edit(int id, Person person)
{
if (String.IsNullOrEmpty(person.Name.First.Trim()))
ModelState.AddModelError("First","First name is required.");
if (String.IsNullOrEmpty(person.Name.Last.Trim()))
ModelState.AddModelError("Name.Last","Last name is required.");
...
}
The validation works for both First and Last in that the Validation Message is properly displayed for the appropriate error. However, the input-validation-error class is not being set on the textbox.
How do I ensure that the input-validation-error class gets set on the textbox when the textbox is tied to a field in a nested class of the Model?
The problem is your call to TextBox uses "Name.First", but your validation message (and validation code) use "First". These names all need to line up. Switch everything to "Name.First" and it will work.
Marked as answer by ricka6 on Jan 19, 2010 07:35 PM
Did you see the Last name in the example though? I had tried both different ways seeing if I could get things to work. The last name is layed out (aside from the label) exactly as you said. The validator text gets displayed in both cases -- first and
last name. The weird thing here is that the textbox itself is not being assigned the class input-validation-error. I wanted my textbox to have a red border with a lightly shaded red background. I have the input-validation-error class defined as such in
my stylesheet but the class is never being assigned to the textbox.
As I mentioned above, I changed "First" to "Name.First" in both the view and the controller so that everything was using "Name.First"
I removed the calls to .Trim() (if you did that with a null value, it would throw a NullReferenceException)
And everything works just fine for me.
Controller:
using System;
using System.Web.Mvc;
public class Person {
public Name Name { get; set; }
public string Gender { get; set; }
}
public class Name {
public string First { get; set; }
public string Last { get; set; }
}
public class HomeController : Controller {
public ViewResult Index() {
return View(
new Person {
Name = new Name {
First = "Brad",
Last = "Wilson"
}
}
);
}
[HttpPost]
public ActionResult Index(Person person) {
if (String.IsNullOrEmpty(person.Name.First))
ModelState.AddModelError("Name.First", "First name is required.");
if (String.IsNullOrEmpty(person.Name.Last))
ModelState.AddModelError("Name.Last", "Last name is required.");
return View(person);
}
}
Thanks so much. Worked like a charm this morning. I think I had Name.First written First.Name in a line of code that was throwing me. Anyhow, thanks a ton for your input!
rodd_harris
Member
82 Points
80 Posts
How do I get input-validation-error css set on textbox of nested object?
Jan 15, 2010 05:58 PM|LINK
I have the following object:
public class Person { public Name Name {get; set;} public string Gender {get; set;} ... } public class Name { public string First {get; set;} public string Last {get; set;} ... }In my edit View, I have the following:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %> <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm()) {%> <label for="First">First:</label> <div class="detail"> <%= Html.TextBox("Name.First", Model.Name.First)%> <%= Html.ValidationMessage("First", "*")%> </div><br /> <label for="Name_Last">Last:</label> <div class="detail"> <%=Html.TextBox("Name.Last", Model.Name.Last)%> <%= Html.ValidationMessage("Name.Last", "*")%> </div><br /> ...Lastly, my validation in my controller looks like this:
public ActionResult Edit(int id, Person person) { if (String.IsNullOrEmpty(person.Name.First.Trim())) ModelState.AddModelError("First","First name is required."); if (String.IsNullOrEmpty(person.Name.Last.Trim())) ModelState.AddModelError("Name.Last","Last name is required."); ... }The validation works for both First and Last in that the Validation Message is properly displayed for the appropriate error. However, the input-validation-error class is not being set on the textbox.
How do I ensure that the input-validation-error class gets set on the textbox when the textbox is tied to a field in a nested class of the Model?
Thanks in advance for your help!
thuhue
All-Star
15625 Points
3146 Posts
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 15, 2010 10:14 PM|LINK
You said:"However, the input-validation-error class is not being set on the textbox."
Please post markup/codebehind for the said textbox in question?
rodd_harris
Member
82 Points
80 Posts
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 16, 2010 01:18 AM|LINK
As far as I'm aware, there isn't code behind an MVC View. I posted all the relevant code.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 16, 2010 01:55 AM|LINK
The problem is your call to TextBox uses "Name.First", but your validation message (and validation code) use "First". These names all need to line up. Switch everything to "Name.First" and it will work.
rodd_harris
Member
82 Points
80 Posts
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 17, 2010 01:58 AM|LINK
Thanks for your reply.
Did you see the Last name in the example though? I had tried both different ways seeing if I could get things to work. The last name is layed out (aside from the label) exactly as you said. The validator text gets displayed in both cases -- first and last name. The weird thing here is that the textbox itself is not being assigned the class input-validation-error. I wanted my textbox to have a red border with a lightly shaded red background. I have the input-validation-error class defined as such in my stylesheet but the class is never being assigned to the textbox.
Any other ideas?
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 18, 2010 07:14 PM|LINK
I took your code and made two changes:
And everything works just fine for me.
Controller:
using System; using System.Web.Mvc; public class Person { public Name Name { get; set; } public string Gender { get; set; } } public class Name { public string First { get; set; } public string Last { get; set; } } public class HomeController : Controller { public ViewResult Index() { return View( new Person { Name = new Name { First = "Brad", Last = "Wilson" } } ); } [HttpPost] public ActionResult Index(Person person) { if (String.IsNullOrEmpty(person.Name.First)) ModelState.AddModelError("Name.First", "First name is required."); if (String.IsNullOrEmpty(person.Name.Last)) ModelState.AddModelError("Name.Last", "Last name is required."); return View(person); } }View:
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm()) { %> <label for="First">First:</label> <div class="detail"> <%= Html.TextBox("Name.First", Model.Name.First) %> <%= Html.ValidationMessage("Name.First", "*") %> </div> <br /> <label for="Name_Last">Last:</label> <div class="detail"> <%= Html.TextBox("Name.Last", Model.Name.Last) %> <%= Html.ValidationMessage("Name.Last", "*") %> </div> <br /> <input type="submit" /> <% } %>And this is the resulting HTML, after I've submitted it with empty first and last names:
<div class="validation-summary-errors"><span>Edit was unsuccessful. Please correct the errors and try again.</span> <ul><li>First name is required.</li> <li>Last name is required.</li> </ul></div> <form action="/" method="post"> <label for="First">First:</label> <div class="detail"> <input class="input-validation-error" id="Name_First" name="Name.First" type="text" value="" /> <span class="field-validation-error">*</span> </div> <br /> <label for="Name_Last">Last:</label> <div class="detail"> <input class="input-validation-error" id="Name_Last" name="Name.Last" type="text" value="" /> <span class="field-validation-error">*</span> </div> <br /> <input type="submit" /> </form>rodd_harris
Member
82 Points
80 Posts
Re: How do I get input-validation-error css set on textbox of nested object?
Jan 19, 2010 03:21 PM|LINK
Brad,
Thanks so much. Worked like a charm this morning. I think I had Name.First written First.Name in a line of code that was throwing me. Anyhow, thanks a ton for your input!
Rodd