asp.net mvc 3 dataanotation external to the database fields in the model not validating
How can i make this work like I have 5 fields in my database table .
I created a model for validation with datannotations and created the compare password field as well with comparepassword attribute. (now my model has 6 fields including one external field compare password and 5 database fields)
I created a view with that model.(User)
Now I created a controller as well but the problem arise here that when calling that
ActionMethod
the compiler complains about the external field and tells it cannot find any extension method. How can I create the extension method or solve this error?[HttpPost] public ActionResult Create(User Users) { }
This is the error I get:
CS1061: 'MvcApplication1.Models.User' does not contain a definition for 'ComparePassword' and no extension method 'ComparePassword' accepting a first argument of type 'MvcApplication1.Models.User' could be found (are you missing a using directive or an assembly reference?)
My Model:
[MetadataType(typeof(UserModel))]
public partial class User { }
public class UserModel
{
[Display(Name = "User Name")]
[Remote("Username", "User", ErrorMessage = "User Name already exists")]
[Required(ErrorMessage = "User Name is required")]
public string vcr_UserName { get; set; }
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be a valid Email Address")]
[Remote("EmailAddress", "User", ErrorMessage = "Email Address already exists")]
[Required(ErrorMessage = "Email is required")]
[Display(Name = "Email Address")]
public string vcr_EmailAddress { get; set; }
[Display(Name = "Password")]
[RegularExpression(@"^.{3,16}$", ErrorMessage = "The Password length must be between 3 and 16 characters ")]
[Required(ErrorMessage = "Password is required")]
public string vcr_Password { get; set; }
[Display(Name = "Compare Password")]
[Compare("vcr_Password", ErrorMessage = "Passwords do not match")]
public string ComparePassword { get; set; }
[Required(ErrorMessage = "Country is required")]
[Display(Name = "Country")]
public string vcr_Country { get; set; }
[Required(ErrorMessage = "Website is required")]
[Display(Name = "WebSite")]
public string vcr_Website { get; set; }
[Displ开发者_Go百科ay(Name = "Expertise")]
[Required(ErrorMessage = "Expertise is required")]
public string vcr_Expertise { get; set; }
public int int_GroupId { get; set; }
public Boolean bit_Active { get; set; }
}
My Controller
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(User Users)
{
try
{
UserRepository UserRep = new UserRepository();
if (ModelState.IsValid)
{
Users.int_GroupId = 2;
Users.dtm_CreatedDate = DateTime.Now;
Users.bit_Active = true;
UserRep.Add(Users);
UserRep.Save();
}
return View();
}
catch { return View(); }
}
View
@model MvcApplication1.Models.User
{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Index
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Users</legend>
<div class="editor-label">
@Html.LabelFor(model => model.vcr_UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.vcr_UserName)
@Html.ValidationMessageFor(model => model.vcr_UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.vcr_Password)
</div>
<div class="editor-field">
@Html.PasswordFor(model => model.vcr_Password)
@Html.ValidationMessageFor(model => model.vcr_Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ComparePassword)
</div>
<div class="editor-field">
@Html.PasswordFor(model => model.ComparePassword)
@Html.ValidationMessageFor(model => model.ComparePassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.vcr_EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.vcr_EmailAddress)
@Html.ValidationMessageFor(model => model.vcr_EmailAddress)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Ok when i click the create i am getting the above error
What do you mean by "external field"?
One thing that already comes to mind is:
Data annotations for view behaviour should not be placed on a model class. You should separate your model facing the database from the viewmodel facing the view. There are numerous articles about that.
If you create a viewmodel, you can add the 6th field there, add the data annotations, have the validation do its thing, etc.
You can use a tool like AutoMapper or ValueInjecter to perform the mapping from model to viewmodel and vice versa, so you don't have to write too much code.
If you have any further questions, feel free to ask...
精彩评论