data-annotations

ASP.NET MVC 3 Custom Display Template With UIHint - For Loop Required?

断了今生、忘了曾经 提交于 2019-12-09 06:07:39
问题 If i have a ViewModel like this: public class MyViewModel { [UIHint("SomeTemplate")] public ICollection<SomeViewModel> Submodel { get; set; } } And a strongly-typed View with a line of HTML like this: @Html.DisplayFor(model => model.Submodel) And a display template with a signature like this: @model MvcApplication1.Models.SomeViewModel I get an error saying "the model item is of type List<SomeViewModel> but this dictionary requires a model of type SomeViewModel .". Which makes sense, but i

Is it possible to use Data Annotations to validate parameters passed to an Action method of a Controller?

。_饼干妹妹 提交于 2019-12-09 02:51:06
问题 I am using Data Annotations to validate my Model in ASP.NET MVC. This works well for action methods that has complex parameters e.g, public class Params { [Required] string Param1 {get; set;} [StringLength(50)] string Param2 {get; set;} } ActionResult MyAction(Params params) { If(ModeState.IsValid) { // Do Something } } What if I want to pass a single string to an Action Method (like below). Is there a way to use Data Annotations or will I have to wrap the string into a class? ActionResult

Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

青春壹個敷衍的年華 提交于 2019-12-09 00:25:44
问题 I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled. My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false) I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete. Code sample: public class Container { public int ContainerID { get; set; } public string Name { get; set; } public virtual ICollection<Output> Outputs { get; set; } } public

ASP.NET Core is using non-default constructor if no public default constructor found

对着背影说爱祢 提交于 2019-12-08 19:29:25
I am writing ASP.NET Core 2.2.0 application hosted on Service Fabric. I have a class which represents a request and I have declared two constructors: public for my own usage and private for serializers: public class MyClass { private MyClass() // for serializer { } public MyClass(string myProperty) // for myself { MyProperty = myProperty ?? throw new ArgumentNullException(nameof(myProperty)); } [Required] public string MyProperty { get; private set; } } Then, I created an API controller: [ApiController] public class MyController { [HttpPut] public async Task<IActionResult> Save([FromBody]

How does one do property validation of a C# class using Data Annotations in .NET Framework 3.5?

扶醉桌前 提交于 2019-12-08 16:01:22
问题 Is there a way in the .NET Framework to hand some method or validator an object instance whose class is decorated with Data Annotations, and receive a collection of errors? I see that there is a way to do this in .NET 4.x. But is there a similar mechanism in .NET 3.5? 回答1: With a bit of reflection, you could build your own validator which scans the ValidationAttributes on the properties you have. It may not be a perfect solution, but if you're limited to using .NET 3.5, this seems like a

.net Regular Expression to match any kind of letter from any language

百般思念 提交于 2019-12-08 15:55:49
问题 Which regular expression can I use to match (allow) any kind of letter from any language I need to match any letter including any diacritics (e.g. á, ü, ñ, etc.) and exclude any kind of symbol (math symbols, currency signs, dingbats, box-drawing characters, etc.) and punctuation characters. I'm using asp.net MVC 2 with .net 4. I've tried this annotation in my view model: [RegularExpression(@"\p{L}*", ... and this one: [RegularExpression(@"\p{L}\p{M}*", ... but client side validation does not

Is there a way to remove attributes from an inherited property?

巧了我就是萌 提交于 2019-12-08 15:51:53
问题 Is it possible to remove attributes from inherited properties? I thought that by using the new keyword I could do so... public class Person { [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } } public class Employee : Person { [Required] public string JobTitle { get; set; } public new string FirstName { get; set; } } ... but this doesnt work at all. This surprises me because the new is specifically there to hide inherited members. 回答1: Your

In MVC 3, I can't get @Html.DisplayFor to render a formatted string

℡╲_俬逩灬. 提交于 2019-12-08 15:11:44
问题 I'm hoping this is quite a simple one, although after lots of Googling, I've not been able to work it out. I'm working on a shopping cart site with MVC 3, and using code-first Entity Framework. The model I'm sending over is a list of Product objects, and each of those objects includes this property: [Required(ErrorMessage = "This is a required field.")] [DataType(DataType.Currency)] [Range(1.00, 500.00, ErrorMessage = "Products can not be free.")] [DisplayFormat(DataFormatString = "{0:C}")]

ASP.NET MVC 2 Numeric value validation

夙愿已清 提交于 2019-12-08 13:31:18
问题 I have this property on a class: public virtual decimal? Number { get; set; } When I'm using it on a form, MVC validates it automatically. If the user enters a letter, naturally an error is returned: "The value 'D' is not valid for Number." How do I change such error message or even control that behavior? I'm not finding the related attribute or something like that. Thank you! 回答1: It is actually not a message that derives from model validation. The message is added to the model state when

How to loop through DataAnnotation’s DisplayName in MVC view?

◇◆丶佛笑我妖孽 提交于 2019-12-08 13:26:48
问题 I want to access DataAnnotation ’s DisplayName and similar GroupName of a model class then loop through in MVC view. For Example let me say one of my model properties are like this public class Person { [Display(Name="Home Phone",GroupName="Home")] public string HomePhone { get; set; } [Display(Name = "Home Address", GroupName = "Home")] public string HomeAddress { get; set; } [Display(Name = "Office Phone", GroupName = "Office")] public string OfficePhone { get; set; } [Display(Name =