I have a form with two text boxes, an email address and a comments box but I would like to add one more text box to be just a simple question answer that must be validated e.g.. 2 + 3 and the user has to enter 5 for it to validate.
I’m using a database first EF model and have a “Buddy” Comments.cs model handling validation for the two fields.
I can add another textbox to my form but I can’t seem to add another object to my Comments.cs model because it then doesn’t represent my actual modal.
Do you need that textbox data saved to the database? If so, then add an extra colum to your database table, then go into your ef designer and update the model schema. It will refresh and add the new property for you. Just add that to your buddy class.
Mm10, your answer was perfect and clean in this instance!
Andrei, I am a little confused as to the difference between a ViewModel and a database modal in this case, my code is below and I kind of though that’s what I was doing???
CodeHobo, thanks again. But I didn’t want the value to go into the database, I was going to take the easy way out and do that. I was also not sure how to retrieve the value from the text box inside the controller as it’s not part of the modal. As for reCaptcha,
I would love to just use that but I can’t read words most of the time and I think in my case would stop people commenting.
So I will see how this goes for now, in a perfect world I wouldn’t have to worry, but some guy in Russia keeps spamming the comment box.
My Buddy class now looks like this,
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace TMPBlog.Models
{
[MetadataType(typeof(CommentMetaData))]
public partial class Comment
{
[Required(ErrorMessage = "Please answer the question!")]
[Range(0, 10, ErrorMessage = "Incorrect Answer")]
public int CommentQuestion { get; set; }
}
public class CommentMetaData
{
[Required(ErrorMessage = "Please enter an email address!")]
[RegularExpression("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", ErrorMessage = "That is not a valid email address!")]
public object CommentEmail { get; set; }
[Required(ErrorMessage = "Please enter a comment!")]
public object CommentDetail { get; set; }
}
}
Andrei, I am a little confused as to the difference between a ViewModel and a database modal in this case, my code is below and I kind of though that’s what I was doing???
CodeHobo, thanks again. But I didn’t want the value to go into the database, I was going to take the easy way out and do that. I was also not sure how to retrieve the value from the text box inside the controller as it’s not part of the modal. As for reCaptcha,
I would love to just use that but I can’t read words most of the time and I think in my case would stop people commenting.
To retrive the value of the textbox all you have to do is pass the name of the textbox as a string input parameter to your action method.
For example:
//View
@Html.TextBox("Verification")
//Controller
[HttpPost]
public ActionResult Create(Comment comment, string Verification){
// Verification now has the value from the textbox, as long as the names match
}
I've been stuck back in the world of Webforms for the past week due to work but was able to get back to this finally. The last few days my world has opened up to ViewModals. I think because I’ve been doing everything via database first I have been relying
completely on the auto generated EF modals. Now MVC is all make a lot more sense,
bojangles
Participant
974 Points
642 Posts
Database first form validation for non model textbox
Apr 19, 2012 10:30 AM|LINK
Hi,
I have a form with two text boxes, an email address and a comments box but I would like to add one more text box to be just a simple question answer that must be validated e.g.. 2 + 3 and the user has to enter 5 for it to validate.
I’m using a database first EF model and have a “Buddy” Comments.cs model handling validation for the two fields.
I can add another textbox to my form but I can’t seem to add another object to my Comments.cs model because it then doesn’t represent my actual modal.
How can I go about something like this?
Cheers,
Mike.
mm10
Contributor
6409 Points
1184 Posts
Re: Database first form validation for non model textbox
Apr 19, 2012 10:38 AM|LINK
Create a partial class of your model and declare the new property in there:
public partial class Model { public string AdditionalProperty{get;set;} }ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: Database first form validation for non model textbox
Apr 19, 2012 10:40 AM|LINK
There is a difference between the Model of a table in the database and the ViewModel of the View.
Please see
http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
( as a solution, add to your ViewModel whatever you need in order to display properly)
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Database first form validation for non model textbox
Apr 19, 2012 05:11 PM|LINK
Do you need that textbox data saved to the database? If so, then add an extra colum to your database table, then go into your ef designer and update the model schema. It will refresh and add the new property for you. Just add that to your buddy class.
It looks like you want a catcha, if that's the case. Just do the validation manually and use ModelState.AddModelError in your controller after you verify your catcha. Also, consider using something like recaptcha
http://ihatethissite.com/blog/2011/02/27/recaptcha-with-asp-net-mvc-razor-using-microsoft-web-helpers/
Blog | Twitter : @Hattan
bojangles
Participant
974 Points
642 Posts
Re: Database first form validation for non model textbox
Apr 20, 2012 12:48 AM|LINK
Thanks all,
Mm10, your answer was perfect and clean in this instance!
Andrei, I am a little confused as to the difference between a ViewModel and a database modal in this case, my code is below and I kind of though that’s what I was doing???
CodeHobo, thanks again. But I didn’t want the value to go into the database, I was going to take the easy way out and do that. I was also not sure how to retrieve the value from the text box inside the controller as it’s not part of the modal. As for reCaptcha, I would love to just use that but I can’t read words most of the time and I think in my case would stop people commenting.
So I will see how this goes for now, in a perfect world I wouldn’t have to worry, but some guy in Russia keeps spamming the comment box.
My Buddy class now looks like this,
using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace TMPBlog.Models { [MetadataType(typeof(CommentMetaData))] public partial class Comment { [Required(ErrorMessage = "Please answer the question!")] [Range(0, 10, ErrorMessage = "Incorrect Answer")] public int CommentQuestion { get; set; } } public class CommentMetaData { [Required(ErrorMessage = "Please enter an email address!")] [RegularExpression("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", ErrorMessage = "That is not a valid email address!")] public object CommentEmail { get; set; } [Required(ErrorMessage = "Please enter a comment!")] public object CommentDetail { get; set; } } }Cheers,
Mike.
ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: Database first form validation for non model textbox
Apr 20, 2012 02:48 AM|LINK
Please read again.
http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels
The ViewModel is from 2 Models from database.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Database first form validation for non model textbox
Apr 20, 2012 04:12 PM|LINK
To retrive the value of the textbox all you have to do is pass the name of the textbox as a string input parameter to your action method.
For example:
//View @Html.TextBox("Verification") //Controller [HttpPost] public ActionResult Create(Comment comment, string Verification){ // Verification now has the value from the textbox, as long as the names match }Blog | Twitter : @Hattan
bojangles
Participant
974 Points
642 Posts
Re: Database first form validation for non model textbox
Apr 21, 2012 01:09 AM|LINK
Thanks CodeHobo,
So simple!
Cheers,
Mike.
bojangles
Participant
974 Points
642 Posts
Re: Database first form validation for non model textbox
May 03, 2012 06:00 AM|LINK
Thanks Andrei,
I've been stuck back in the world of Webforms for the past week due to work but was able to get back to this finally. The last few days my world has opened up to ViewModals. I think because I’ve been doing everything via database first I have been relying completely on the auto generated EF modals. Now MVC is all make a lot more sense,
Cheers,
Mike.