modelstate

C# .NET MVC3 ModelState.IsValid

落爺英雄遲暮 提交于 2019-12-03 05:11:37
问题 Im using JSON to post data from a form and ModelState.isValid() returning false, i put a WriteLine for all incoming data and everything looks fine data wise, is there a way to display model state errors to figure out what is not validating? this exact code works fine with other models [HttpPost] public ActionResult mobileCreateAction(Trip trip) { if (ModelState.IsValid) { System.Diagnostics.Debug.WriteLine("saving"); DB.Trips.Add(trip); DB.SaveChanges(); return Json(new { success = true, msg

How to get a ModelState key of an item in a list

帅比萌擦擦* 提交于 2019-12-03 04:54:30
Problem I have a list of fields that the user can edit. When the model is submitted I want to check if this items are valid. I can't use data notations because each field has a different validation process that I will not know until runtime. If the validation fails I use the ModelState.AddModelError(string key, string error) where the key is the name of the html element you want to add the error message to. Since there are a list of fields the name that Razor generates for the html item is like Fields[0].DisplayName . My question is there a method or a way to get the key of the generated html

ModelState.IsValid is false prior to validation

随声附和 提交于 2019-12-02 11:24:18
We wrote a custom model binder that overrides the CreateModel method of the ComplexTypeModelBinder so that we can have injection into our ViewModels instead of having to pass the injected clients and repos to our model from the controller . For example, for a model like this: public class ThingViewModel { public ThingViewModel (IThingRepo thingRepo) {} } In our controller we can do: public class ThingController : Controller { public IActionResult Index(ThingViewModel model) => View(model); } And this works pretty well, here's the override part of the custom model binder : protected override

IOC on IValidationDictionary with Castle Windsor

狂风中的少年 提交于 2019-12-02 00:08:28
I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller. I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constructor so that in my code I can pass the following as an example: IMembershipService _memSvc; IValidationDictionary _validationService; public AccountController() { _validationService = new ModelStateWrapper(this.ModelState); _memSvc = new MembershipService(

Data annotations MVC3 Required attribute

余生长醉 提交于 2019-12-01 20:06:48
I have the Model (User) below, I use it to add new users and to update existing users. When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's not allowed to change the password. Here is the problem, adding a new user everything works ok because I enter both name and password values so ModelState.IsValid returns true, but when updating an user there is no input to the password, so it always have the null value what makes the ModelState.IsValid returns false all the time. Is there a way to use

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

半腔热情 提交于 2019-11-30 20:32:12
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 = false, ErrorMessage = "Do not allow empty strings")] [DisplayName("Hobby")] [DataType(DataType.Text)]

ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

落花浮王杯 提交于 2019-11-30 14:58:53
问题 I have tried to make this work and made many google/stackoverflow searches with no luck at all. I have a simple Model: public class MovieModel { public string Id { get; set; } [Required] [StringLength(100)] public string Name { get; set; } } A method in the controller: // POST: api/Movies public IHttpActionResult Post([FromBody]MovieModel movieModel) { if (ModelState.IsValid) { //Code } } And a test method (is an integration test, but the same would happen in unit tests): [TestMethod] public

ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 12:48:54
I have tried to make this work and made many google/stackoverflow searches with no luck at all. I have a simple Model: public class MovieModel { public string Id { get; set; } [Required] [StringLength(100)] public string Name { get; set; } } A method in the controller: // POST: api/Movies public IHttpActionResult Post([FromBody]MovieModel movieModel) { if (ModelState.IsValid) { //Code } } And a test method (is an integration test, but the same would happen in unit tests): [TestMethod] public void MoviesController_Post_Without_Name() { // Arrange var model = new MovieModel(); model.Name = ""; /