asp.net-mvc-2-validation

How to display MVC 3 client side validation results in validation summary

橙三吉。 提交于 2019-12-03 02:05:33
I have a registration form on which I use client side validation (Required, StringLength etc. specified on my view model). The form is currently pretty much how the scaffolder creates it: @using (Html.BeginForm("Index", "Registration")) { @Html.ValidationSummary(true) <fieldset> <legend>Registration details</legend> @Html.ValidationSummary(false, "Please correct these errors:") @Html.ValidationMessageFor(model => model.Username) <div class="editor-label"> @Html.LabelFor(model => model.Username) </div> <div class="editor-field"> @Html.EditorFor(model => model.Username) </div> <p> <input type=

ASPNET MVC - Why is ModelState.IsValid false “The x field is required” when that field does have a value?

元气小坏坏 提交于 2019-12-01 05:14:00
I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string OrderNumber {get;set;} // etc. } When I submit an order from the view (using $.post, not input type=submit) it goes to my controller class: public class PurchaseOrderController { public JsonResult Save(PurchaseOrder order) { // TryUpdateModel(order); // commented out since modelstate.isvalid remains false anyway if (ModelState.IsValid) { // its never valid } } } ModelState.IsValid always returns false, with the error: "The Order Number field is required." But there is a value in this field (??

ASPNET MVC - Why is ModelState.IsValid false “The x field is required” when that field does have a value?

烂漫一生 提交于 2019-12-01 02:13:35
问题 I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string OrderNumber {get;set;} // etc. } When I submit an order from the view (using $.post, not input type=submit) it goes to my controller class: public class PurchaseOrderController { public JsonResult Save(PurchaseOrder order) { // TryUpdateModel(order); // commented out since modelstate.isvalid remains false anyway if (ModelState.IsValid) { // its never valid } } } ModelState.IsValid always

Default resource for data annotations in ASP.NET MVC

若如初见. 提交于 2019-11-30 00:18:08
There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources); then [Required] public string Name { get; set; } someone gimme a light! thanks in advance EDIT My real problem was with EF Code First CTP4. CTP5 fix it. Thanks for everyone. You could try doing this: Add this class

Default resource for data annotations in ASP.NET MVC

走远了吗. 提交于 2019-11-28 21:17:50
问题 There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources); then [Required] public string Name { get; set; } someone gimme a light! thanks in advance EDIT My real problem was

ASP.Net MVC 2 Controller's TryValidate doesn't validate the List<> items within the model

懵懂的女人 提交于 2019-11-28 12:42:27
How do you get a model's validation to also validate child objects in a generic list property. I have a model that I'm trying to validate, this is not what's being posted to the server, but a composite of some information posted, and information already on the server... for example. ... public class A { [Required] public string Property1 { get; set; } } ... public class B { public List<A> Values { get; set; } } ... if (!TryValidateModel(instanceofB)) { //this should fire, as one of A inside B isn't valid. return View(instanceofB); } When I try to validate the model instance of B, it won't

ASP.Net MVC 2 Controller's TryValidate doesn't validate the List<> items within the model

倾然丶 夕夏残阳落幕 提交于 2019-11-27 07:11:21
问题 How do you get a model's validation to also validate child objects in a generic list property. I have a model that I'm trying to validate, this is not what's being posted to the server, but a composite of some information posted, and information already on the server... for example. ... public class A { [Required] public string Property1 { get; set; } } ... public class B { public List<A> Values { get; set; } } ... if (!TryValidateModel(instanceofB)) { //this should fire, as one of A inside B

ModelState.AddModelError - How can I add an error that isn't for a property?

久未见 提交于 2019-11-27 06:07:00
I am checking my database in Create(FooViewModel fvm){...} to see if the fvm.prop1 and fvm.prop2 already exist in that combination; if so, I want to add an error to the modelstate, then return the whole view. I tried: public ActionResult Create(FooViewModel fvm){ if (ThatComboAlreadyExists(fvm)) { ModelState.AddModelError("Model", "There is already one like that"); return View(fvm); } } ...but I get no display of errors in the Html.ValidationSummary , which is where I assume they would appear. I have the suspicion that "Model" is not the right key, but I haven't been able to find anything a la

ModelState.AddModelError - How can I add an error that isn&#39;t for a property?

白昼怎懂夜的黑 提交于 2019-11-26 11:53:39
问题 I am checking my database in Create(FooViewModel fvm){...} to see if the fvm.prop1 and fvm.prop2 already exist in that combination; if so, I want to add an error to the modelstate, then return the whole view. I tried: public ActionResult Create(FooViewModel fvm){ if (ThatComboAlreadyExists(fvm)) { ModelState.AddModelError(\"Model\", \"There is already one like that\"); return View(fvm); } } ...but I get no display of errors in the Html.ValidationSummary , which is where I assume they would

ASP.NET MVC Html.ValidationSummary(true) does not display model errors

房东的猫 提交于 2019-11-26 04:59:56
问题 I have some problem with Html.ValidationSummary. I don\'t want to display property errors in ValidationSummary. And when I set Html.ValidationSummary(true) it does not display error messages from ModelState. When there is some Exception in controller action on string MembersManager.RegisterMember(member); catch section adds an error to the ModelState: ModelState.AddModelError(\"error\", ex.Message); But ValidationSummary does not display this error message. When I set Html.ValidationSummary