modelstate

How to remove a single error from a property with multiple validation

本秂侑毒 提交于 2019-12-23 04:56:17
问题 In my ViewModel I have a property with multiple validation errors like this [Required(ErrorMessage = "Required")] [RegularExpression(@"^(\d{1,2})$", ErrorMessage = "Only numbers admitted")] [MaxLength(3, ErrorMessage = "MaxLength is 3")] public string MyField { get; set; } The "Required" validation depends on user input on another field, so in the controller I want to remove the error from the ModelState. The only way to do it that I found is like this: if (view.OtherField != null && view

ASP.NET MVC ModelState.IsValid doesnt work

感情迁移 提交于 2019-12-22 12:54:58
问题 I've this controller's method for create [HttpPost] public ActionResult Create(Topic topic) { if (ModelState.IsValid) { topicRepo.Add(topic); topicRepo.Save(); return RedirectToAction("Details", new { id = topic.ID }); } return View(topic); } and this for edit [HttpPost] public ActionResult Edit(int id, FormCollection formCollection) { Topic topic = topicRepo.getTopic(id); if (ModelState.IsValid) { UpdateModel<Topic>(topic); topicRepo.Save(); return RedirectToAction("Details", new { id =

ModelState is coming up as invalid?

扶醉桌前 提交于 2019-12-22 12:38:08
问题 I'm working on a MVC5 Code-First application. On one Model's Edit() view I have included [Create] buttons to add new values to other models from within the Edit() view and then repopulate the new value within DropDownFors() on the Edit() . For this first attempt, I am passing a model_description via AJAX to my controller method createNewModel() : [HttpPost] public JsonResult createNewModel(INV_Models model) { // model.model_description is passed in via AJAX -- Ex. 411 model.created_date =

ModelState is always considered valid, regardless of null values in required fields

ε祈祈猫儿з 提交于 2019-12-22 04:09:34
问题 I've been looking around and I think my solution is just fine but somehow the ModelState.IsValid property is always true . Consider the following code snippets: [Route("address")] [HttpPut] [ResponseType(typeof(UserViewModel))] public IHttpActionResult UpdateAddress([FromBody] UpdateAdressValidationModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // irrelevant code omitted } [TestMethod] public void UpdateAddress_WithoutStreet_ReturnsHttpCode400() { var

In Web API.Core, how to do Model Validation in every method?

天大地大妈咪最大 提交于 2019-12-21 04:02:51
问题 I am getting into ASP.Net Core 2.0 with Web API. One of my first methods are my login: /// <summary> /// API endpoint to login a user /// </summary> /// <param name="data">The login data</param> /// <returns>Unauthorizied if the login fails, The jwt token as string if the login succeded</returns> [AllowAnonymous] [Route("login")] [HttpPost] public IActionResult Login([FromBody]LoginData data) { var token = _manager.ValidateCredentialsAndGenerateToken(data); if (token == null) { return

Multiple forms in MVC view: ModelState applied to all forms

前提是你 提交于 2019-12-20 10:37:27
问题 Running into some trouble with multiple forms on a single view. Suppose I have the following viewmodel: public class ChangeBankAccountViewModel { public IEnumerable<BankInfo> BankInfos { get; set; } } public class BankInfo { [Required] public string BankAccount { get; set; } public long Id { get; set; } } In my viewmodel, I want all BankInfos to be displayed underneath eachother, inside separate forms for each. To achieve this, I'm using a partial view _EditBankInfo: @model BankInfo @using

IOC on IValidationDictionary with Castle Windsor

白昼怎懂夜的黑 提交于 2019-12-20 02:58:26
问题 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

How to render errors to client? AngularJS/WebApi ModelState

最后都变了- 提交于 2019-12-18 11:34:12
问题 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 =

How to read modelstate errors when returned by Json?

≡放荡痞女 提交于 2019-12-17 17:27:03
问题 How can I display ModelState errors returned by JSON? I want to do something like this: if (!ValidateLogOn(Name, currentPassword)) { ModelState.AddModelError("_FORM", "Username or password is incorrect."); //Return a json object to the javascript return Json(new { ModelState }); } What must be my code in the view to read the ModelState errors and display them? My actual code in the view to read the JSON values is as follows: function createCategoryComplete(e) { var obj = e.get_object(); alert

ASP.NET MVC How to convert ModelState errors to json

元气小坏坏 提交于 2019-12-17 15:01:26
问题 How do you get a list of all ModelState error messages? I found this code to get all the keys: ( Returning a list of keys with ModelState errors) var errorKeys = (from item in ModelState where item.Value.Errors.Any() select item.Key).ToList(); But how would I get the error messages as a IList or IQueryable? I could go: foreach (var key in errorKeys) { string msg = ModelState[error].Errors[0].ErrorMessage; errorList.Add(msg); } But thats doing it manually - surely there is a way to do it using