I’m new to MVC and am unsure about proper design.
I have class objects which I use in a variety of applications. I have taken the approach to write a custom view model class so that I can have access to properties in all of these objects and have strong typing. Without re-typing all my class code in the view model is there any way to have the properties in these objects validated using data annotation? Please let me know if my approach and design is all wrong.
[Required]
public User user = new User("username");
//User has lots properites and methods, could i validate inside my class code?
//What I’d like to avoid is putting the following stuff in my custom view model class, //since I already have a class library with this stuff:
public class User
{
[Required]
[StringLength(160)]
public string prop1 { get; set; }
[Required]
[StringLength(160)]
public string prop2 { get; set; }
[Required]
[StringLength(160)]
public string prop3 { get; set; }
public User(string token)
{
SetUser(token);
}
public void SetUser(string token)
{
this.prop1 = "this";
this.prop2 = "this2";
this.prop3 = "this3";
}
============
Good to know I can, but I’m stumbling on some issues. In my view I have: @Html.EditorFor(modelItem => modelItem.user.prop1)
I put the data annotation stuff in my class domain. When it renders it does show the annoations.
<input class="text-box single-line" data-val="true" data-val-length="The field prop1 must be a string with a maximum length of 5." data-val-length-max="5" data-val-required="The prop1 field is required." id="user_prop1" name="user.prop1" type="text" value="somevalue" />
but when I go to my controller the parameter is null. I think because the name is user.prop1. I tried a textbox where i specified the name attribute and yet my controller still couldn’t get a value for my parameter.
====================
@model TrainingCalendar.Models.Training
@{
ViewBag.Title = "Signup";
}
<h2>Signup</h2>
<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>
@using (Html.BeginForm("ConfirmSignup", "Training", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Training</legend>
<p>
@Html.Label("date", Model.SpecifiedCourse.strClassDate)
</p>
<p>
@Html.Label("time", Model.SpecifiedCourse.Time)
</p>
<p>
@Html.Label("instructor", Model.SpecifiedCourse.Instructor)
</p>
<p>
@Html.Hidden("id", Model.SpecifiedCourse.ID)
</p>
<table>
<tr>
<td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop1)</td>
<td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop1)</td>
<td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop1)</td>
</tr>
<tr>
<td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop2)</td>
<td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop2)</td>
<td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop2)</td>
</tr>
</table>
<p>
<input type="submit" value="Sign Up" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
===================
public ActionResult ConfirmSignup(
int id,
string prop1,
string prop2)
{
SignUpForClass();
return View();
}
Absolutely you can have the data annotation attributes on your class code. If you’re encapsulating the class in your view model, populated the properties of the encapsulated class in your view. During the validation process the class members will be validated against the data annotation attributes that you specify in the class declaration.