data-annotations

MVC 4 custom data annotations read in T4 scaffolding

倖福魔咒の 提交于 2019-12-06 14:42:24
Can you create custom data annotations for the model that can be read inside the T4 template for the View like property.Scaffold is read? I would like to add data annotation parameters like Scaffold based on which I would build the view. Thank you Johnie Karr I wrote a blog post on the solution I came up with for MVC5. I'm posting it here for anyone who comes along: https://johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/ Edit: In your entities, decorate property with custom Attribute namespace CustomViewTemplate.Models { [Table("Person")] public class

Why MVC Model Binder set required for int, long values?

余生颓废 提交于 2019-12-06 14:17:03
I have a Model Like this public int Id {get;set;} [Required] public string FirstName{get; set} [Required] public string LastName{get; set} The Id is auto generate in DB. when I want call Create action The ModelState says that "The Id field is required"!!!! I Found this for my problem but it not clean solution. Is there an other way to solve this? Is there a way that I change Mvc ModelBinder behavior for value types? The best solution to this problem is to use a view model. A view model is a class that you specifically design to meet the requirements of your view. So your controller action will

Validation on int data type asp .net mvc3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 13:12:34
问题 I am getting The value 'abc' is not valid for fieldName. as error message. which is default error message and i want to override it in easier way. as of now what i have tried is listed below [RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")] [Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource), ErrorMessageResourceName = "error_numeric")] [RegularExpression("([1-9][0-9]*)")] Range(1,int.max,ErrorMessage="enter numeric value") but failed to change default

Data Annotation with Bools

≯℡__Kan透↙ 提交于 2019-12-06 13:08:04
问题 Down below I have three different categories. How would I structure the validation to make sure that at least one boolean gets selected per category? //Disabilities [Display(Name = "Learning Disabilities")] public bool LD { get; set; } [Display(Name = "Developmental Disabilities")] public bool DD { get; set; } [Display(Name = "AD/HD")] public bool ADHD { get; set; } [Display(Name = "Autism")] public bool Autism { get; set; } //Age Group [Display(Name = "Child")] public bool child { get; set;

ASP.NET MVC Web API data annotation regular expression for email

爱⌒轻易说出口 提交于 2019-12-06 13:05:28
Using [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")] attribute never validates an email property of my model. I'm new to regular expressions so I can't figure out what is the problem. Little help? Look at this article: How To Validate Email Address With Regular Expression . The guy not only providing you with the regular expression, but also explaining step by step what it means. Not only its useful as an expression, but you can also learn a thing or two from this article. Hope this helps. Please let me know if not. [Required] [Display(Name = "Email Address")]

MVC4 - Unobtrusive validation starting too early

不打扰是莪最后的温柔 提交于 2019-12-06 12:08:40
问题 I have a HTML form that uses unobtrusive validation in my website which is an ASP.Net MVC4 application. The form from my view is simple and is shown below @Using (Html.BeginForm()) { @: Listing for @ViewBag.itemNumber @Html.TextBoxFor(model=>model.Name) @Html.TextBoxFor(model=>model.Location) @:<input type="submit" value="Save" /> } My controller is as shown below public ActionResult test() { string itemNumber = Request.params["item"]; ViewBag.itemNumber = itemNumber; return View(); } and my

step by step problem with validation on “back” button

十年热恋 提交于 2019-12-06 10:19:01
I have a 3 steps forms where I bind for each step on a ViewModel that has DataAnnotation. Everything works fine except that when I click on my "previous" button, the validation gets called and, if there's missing values, the user has to fill all required fields. If I go with an action link, then the data won't be persisted. What I'm looking for here is a way to save my values without calling the validation. I also consider using cookies but I don't think that it's the best way of doing it. Any ideas or suggestions? I use separate models for each step of my wizard pages. I also make sure that

asp.net mvc int property bind exception

ε祈祈猫儿з 提交于 2019-12-06 09:18:45
I have a int property in my class and want to validate if the user has entered a string. How can I do that using data annotations? When I pass a non-integer value I get a excpetion like this: The value 'asdasd' is not valid for property. For example using this validation attribute: [Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")] public int? Number { get; set; } and entering 'aaa' in a field tha uses the model I've got an excepetion with this message: The value 'aaa' is not valid for Number. Instead of the "Invalid Number" message. Any Ideas? I've put [Range(0, Int32.MaxValue,

MVC3 Attribute validation question

萝らか妹 提交于 2019-12-06 08:28:33
I'm getting odd behavior with my validation in my view. My model has this property. [Display(Name = "Overflow Capacity")] [RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")] [Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")] public decimal OverFlowCapacity { get; set; } My view has this: <tr> <td>@Html.LabelFor(m=> m.OverFlowCapacity)</td> <td>@Html.EditorFor(m=>m.OverFlowCapacity)</td> <td> @Html.ValidationMessageFor(model => model.OverFlowCapacity)</td> </tr> If I enter a value like 'ABC', I get the validation message 'Number required' If

using Metadata with Entity Framework to validate using Data Annotation

被刻印的时光 ゝ 提交于 2019-12-06 08:27:34
问题 I have a entity called Product,this is a part of it's declration: [EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class Product : EntityObject { #region Factory Method /// <summary> /// Create a new Product object. /// </summary> /// <param name="productID">Initial value of the ProductID property.</param> /// <param name="productName">Initial value of the ProductName property.</param> /// <param