modelstate

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

时光总嘲笑我的痴心妄想 提交于 2019-11-30 05:05:16
问题 We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me. As an example I have a model such as: public class HobbyModel { [Required(AllowEmptyStrings =

How to render errors to client? AngularJS/WebApi ModelState

夙愿已清 提交于 2019-11-30 03:40:47
I'm building an AngularJS SPA application with WebApi for the backend. I am using attributes for model validation on the server, if validation fails this is what I return from the ModelState. {"Message":"The request is invalid.","ModelState":{"model.LastName":["Last Name must be at least 2 characters long."]}} How do I then render this to the client with AngularJS? //Save User Info $scope.processDriverForm = function(isValid) { if (isValid) { //set button disabled, icon, text $scope.locked = true; $scope.icon = 'fa fa-spinner fa-spin'; $scope.buttonText = 'Saving...'; $scope.submitted = true;

ModelState.IsValid is always returning false [duplicate]

会有一股神秘感。 提交于 2019-11-30 01:36:19
This question already has an answer here: ModelState.IsValid == false, why? 8 answers [HttpPost] public ActionResult Create(Users user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } return View(user); } ModelState.IsValid is always false. so it just return view and new record is not getting added.. Edit User: public class User { public int UserID { get; set; } public string Name { get; set; } [Display(Name = "Confirm Password")] [DataType(DataType.Password)] public string ConfirmPassword { get; set; } public string Designation { get; set;

How do I access the ModelState from an ActionFilter?

Deadly 提交于 2019-11-30 01:16:59
I'm building an ActionFilter to reuse some code for a simple spam block - basically what I do is that I have a Html Helper method that renders an input textbox and a hidden input, and in the ActionFilter I check whether the two values are the same or not. If not, I want to leverage the rest of my validation logic and add a ModelStateError to the ModelState , but how do I do that? How do I add a ModelStateError from whithin the ActionFilter ? UPDATE: Here's the code I'm trying with. When I test a controller action that has this attribute, ModelState.IsValid still returns true even though I don

ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

混江龙づ霸主 提交于 2019-11-29 20:09:59
I have written my first unit tests for an ASP.NET MVC web application. All works fine and it is giving me valuable information, but I can't test errors in the view model. The ModelState.IsValid is always true, even when some values are not filled in (empty string or null). I have read already that the model validation happens when the posted data is mapped to the model and you need to write some code to do the model verification yourself: Geek With Blogs so: How can U test ModelState? I have tried the three examples provided in the linked webpages, but it seems not to work for me. Some code:

Custom model binding, model state, and data annotations

半世苍凉 提交于 2019-11-28 23:48:55
I have a few questions regarding custom model binding, model state, and data annotations. 1) Is it redundant to do validation in the custom model binder if I have data annotations on my model, because that's what I thought the point of data annotations were. 2) Why is my controller treating the model state as valid even when it's not, mainly I make the Name property null or too short. 3) Is it ok to think of custom model binders as constructor methods, because that's what they remind me of. First here is my model. public class Projects { [Key] [Required] public Guid ProjectGuid { get; set; }

ModelState.IsValid is always returning false [duplicate]

二次信任 提交于 2019-11-28 22:24:04
问题 This question already has an answer here: ModelState.IsValid == false, why? 8 answers [HttpPost] public ActionResult Create(Users user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } return View(user); } ModelState.IsValid is always false. so it just return view and new record is not getting added.. Edit User: public class User { public int UserID { get; set; } public string Name { get; set; } [Display(Name = "Confirm Password")]

How do I access the ModelState from an ActionFilter?

♀尐吖头ヾ 提交于 2019-11-28 20:51:20
问题 I'm building an ActionFilter to reuse some code for a simple spam block - basically what I do is that I have a Html Helper method that renders an input textbox and a hidden input, and in the ActionFilter I check whether the two values are the same or not. If not, I want to leverage the rest of my validation logic and add a ModelStateError to the ModelState , but how do I do that? How do I add a ModelStateError from whithin the ActionFilter ? UPDATE: Here's the code I'm trying with. When I

ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

强颜欢笑 提交于 2019-11-28 15:48:11
问题 I have written my first unit tests for an ASP.NET MVC web application. All works fine and it is giving me valuable information, but I can't test errors in the view model. The ModelState.IsValid is always true, even when some values are not filled in (empty string or null). I have read already that the model validation happens when the posted data is mapped to the model and you need to write some code to do the model verification yourself: Geek With Blogs so: How can U test ModelState? I have

Updating value provider prior to TryUpdateModel

感情迁移 提交于 2019-11-28 13:50:31
Lets say we have a class with a property called PetsName. If it is left blank on the screen I want to update the value provider so if the user doesn't enter a pet name, we force 'unnamed'. This isn't the actual scenario.. this is of course a sample, so answers like 'just set default values on a webpage, etc' won't fit this scenario : ) The main issue is we want to update the values so when you update the model it will use whatever you have overridden. I guess one idea is to remove the value and add it. When I check ModelState, it does have the updated value, however when I call TryUpdateModel,