In my project we have some editor templates that render html like the following:
<table>
<tr>
<td><image src="/SIIJYP/Content/Images/lupa.bmp" id="@(controlId)img"/></td>
<td>
@Html.Hidden("Id", Model.Id)
@Html.TextBox("Fullname", Model.Fullname, new {@readonly="readonly"})
</td>
</tr>
</table>
This looks like a little image next to a textbox and it is used to allow searching and selecting a person from a people search pop-up window.
Models which use this template have properties as follow:
public class MyModel
{
[Required(ErrorMessage="This value is required")]
[UIHint("PeopleSearch")]
public Person Responsible { get; set;}
}
All works perfectly, template is rendered propertly, values are posted, model is validated, etc. However, when user does not select a person and posts, the expected error message “This value is required” is not displayed.
How should we modify our template to display the message when any value is provided by the users?
UPADATE: Person model definition:
public class Person
{
public long Id { get; set;}
public string Fullname { get; set;}
}
What I need is a way that allows me to use the RequiredAttribute in the Responsible property to validate that both: Id and Fullname have values and display the error message below the textbox otherwise.
As stated in the comments, you forgot to put in the html