I have the following code that inserts a record into a database and its working fine but I would like to add some form validation to it but all the examples I can find on the Net are code first. How should I go about doing this via database first?
Controller Code,
public ActionResult Index(Comment comment)
{
if (ModelState.IsValid)
{
string CommentEmail = comment.CommentEmail;
string CommentDetail = comment.CommentDetail;
CommentDetail = CommentDetail.Replace("\n", "<br />");
comment.CommentDetail = CommentDetail;
db.Comments.AddObject(comment);
db.SaveChanges();
return RedirectToAction("CommentResponse", new { id = comment.CommentID });
}
ViewBag.PostCommentFK = new SelectList(db.Posts, "PostID", "PostTitle", comment.PostCommentFK);
return View(comment);
}
You implement this by using the concept of a buddy class. Basically you create another class with public properties that match the properties of your EF model. You apply the validation attributes to your "buddy class".
Then create a partial class to match your EF model (EF generates partial classes for all models). You add an attribute to your partial that points to your "buddy class". Basically you are telling mvc to use the validations in the associated class instead
of the EF model.
I’m trying to follow the tutorial you have recommended but I’m having trouble with the references. This is an MVC4 project and I already have a reference to,
System.ComponentModel.DataAnnotations
but I can’t seem to find
Microsoft.Web.Mvc.DataAnnotations.dll
Anywhere on the Net. I have been able to download Microsoft.Web.Mvc but when I add,
ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
To my Global.asax.cs file I am missing a reference at DataAnnotations.
What should I be doing here, i'm also guessing things may be a little different in MVC4?
I have also downloaded Data
Annotations Model Binder Sample but it wont compile in VWD 2010 Express "This project is not supported by this installation"
Sorry I should have been more clearn. You don't need to downlowd Microsoft.Web.Mvc.DataAnnontations. That part of the post is old and pertained to an earlier version of mvc. In recent versions, including 4, evertyhing you need is in System.ComponentModel.DataAnnonations.
You don't have to download a dll or create a new model binder, everything you need is is already on your system!
All you need to do is follow the instructions for using buddy classes as defined in the bottom section of the tutorial (you can completely ignore anything before the entity framework validation section). Add a reference to System.ComponentModel.DataAnnonations
and you're good to go.
You have been a great help yet again. I kind of thought I may not need to add a reference to
Microsoft.Web.Mvc.DataAnnotations.dll as I was not getting any errors. I just wanted to make sure I was set to go, reference wise before continuing.
bojangles
Participant
974 Points
642 Posts
Database First Form Validation MVC4
Apr 01, 2012 01:15 AM|LINK
Hi,
I have the following code that inserts a record into a database and its working fine but I would like to add some form validation to it but all the examples I can find on the Net are code first. How should I go about doing this via database first?
Controller Code,
public ActionResult Index(Comment comment) { if (ModelState.IsValid) { string CommentEmail = comment.CommentEmail; string CommentDetail = comment.CommentDetail; CommentDetail = CommentDetail.Replace("\n", "<br />"); comment.CommentDetail = CommentDetail; db.Comments.AddObject(comment); db.SaveChanges(); return RedirectToAction("CommentResponse", new { id = comment.CommentID }); } ViewBag.PostCommentFK = new SelectList(db.Posts, "PostID", "PostTitle", comment.PostCommentFK); return View(comment); }View,
@model TMPBlog.Models.Post @using (Html.BeginForm()) { @Html.ValidationSummary(true) @Html.Hidden("CommentDate", String.Format("{0:yyyy-MM-dd HH:mm}", DateTime.Now)) @Html.Hidden("PostCommentFK", @Html.DisplayFor(model => model.PostID)) <br /> <span class="BlueHeading">Add Your Comments Here!</span> <br /><br /> <div class="editor-label"> Email Address </div> <div class="editor-field"> @Html.Editor("CommentEmail") @Html.ValidationMessage("CommentEmail") </div> <div class="editor-label"> Comments </div> <div class="editor-field"> @Html.TextArea("CommentDetail", new { cols = "65", rows = "7" }) </div> <div class="editor-field"> @Html.CheckBox("CommentTicked") Email me when others comment </div> <p> <input type="submit" value="Add Comment" /> </p> }Cheers,
Mike.
validation
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Database First Form Validation MVC4
Apr 01, 2012 02:53 AM|LINK
You implement this by using the concept of a buddy class. Basically you create another class with public properties that match the properties of your EF model. You apply the validation attributes to your "buddy class".
Then create a partial class to match your EF model (EF generates partial classes for all models). You add an attribute to your partial that points to your "buddy class". Basically you are telling mvc to use the validations in the associated class instead of the EF model.
Take a look at this
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
In particular look at the section
Using Data Annotation Validators with the Entity Framework
Blog | Twitter : @Hattan
bojangles
Participant
974 Points
642 Posts
Re: Database First Form Validation MVC4
Apr 01, 2012 11:06 PM|LINK
Thanks CodeHobo,
I’m trying to follow the tutorial you have recommended but I’m having trouble with the references. This is an MVC4 project and I already have a reference to,
System.ComponentModel.DataAnnotations
but I can’t seem to find
Microsoft.Web.Mvc.DataAnnotations.dll
Anywhere on the Net. I have been able to download Microsoft.Web.Mvc but when I add,
To my Global.asax.cs file I am missing a reference at DataAnnotations.
What should I be doing here, i'm also guessing things may be a little different in MVC4?
I have also downloaded Data Annotations Model Binder Sample but it wont compile in VWD 2010 Express "This project is not supported by this installation"
Cheers,
Mike.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Database First Form Validation MVC4
Apr 02, 2012 12:51 AM|LINK
Sorry I should have been more clearn. You don't need to downlowd Microsoft.Web.Mvc.DataAnnontations. That part of the post is old and pertained to an earlier version of mvc. In recent versions, including 4, evertyhing you need is in System.ComponentModel.DataAnnonations.
You don't have to download a dll or create a new model binder, everything you need is is already on your system!
All you need to do is follow the instructions for using buddy classes as defined in the bottom section of the tutorial (you can completely ignore anything before the entity framework validation section). Add a reference to System.ComponentModel.DataAnnonations and you're good to go.
Blog | Twitter : @Hattan
bojangles
Participant
974 Points
642 Posts
Re: Database First Form Validation MVC4
Apr 02, 2012 01:14 AM|LINK
Thanks CodeHobo,
You have been a great help yet again. I kind of thought I may not need to add a reference to Microsoft.Web.Mvc.DataAnnotations.dll as I was not getting any errors. I just wanted to make sure I was set to go, reference wise before continuing.
Cheers,
Mike.