modelstate

asp.net-mvc2 - Strongly typed helpers not using Model?

风流意气都作罢 提交于 2019-11-28 10:50:22
问题 When using strongly typed helpers in MVC2 the input field values aren't taken from the Model property when a post is made. Is this default behavior? (strongly typed) view with strongly typed helpers: <div class="editor-label"> <%: Html.LabelFor(model => model.Name) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Name) %> <%: Html.ValidationMessageFor(model => model.Name) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Price) %> </div> <div class=

Testing ModelState is always valid in asp.net mvc

妖精的绣舞 提交于 2019-11-28 08:59:49
When testing my controller's actions the ModelState is always valid. public class Product { public int Id { get; set; } [Required] [StringLength(10)] public string Name { get; set; } [Required] public string Description { get; set; } [Required] public decimal Price { get; set; } } And my controller. public class ProductController : Controller { [HttpPost] public ActionResult Create(Product product) { if (ModelState.IsValid) { // Do some creating logic... return RedirectToAction("Display"); } return View(product); } } And test: [Test] public TestInvalidProduct() { var product = new Product();

How to read modelstate errors when returned by Json?

前提是你 提交于 2019-11-28 03:21:22
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(obj.Values); } If you are returning JSON, you cannot use ModelState. Everything that the view needs

Is it correct way to use ModelState.Remove to deal with ModelState?

大憨熊 提交于 2019-11-27 21:17:47
Im working on a big MVC3 web application and have an annoyance regarding the ModelState.IsValid method. ModelState is being used in nearly all of my controllers so to validate the data being posted. The views are all based on ViewModels which contain different classes and these classes obviously contain properties which could be marked as [Required] . The problem i am having is the required properties are sometimes not required and im having to use the ModelState.Remove method so that ModelState.IsValid becomes true. My question is by using ModelState.Remove , is this the correct way of doing

MVC3 Remove ModelState Errors

血红的双手。 提交于 2019-11-27 17:46:54
I've got a situation where I'm uploading an image the user has selected from his local file system. My Form in my view, basically has two submit buttons. One is used to Submit the form normally, and all validation executes. The 2nd is only for uploading the image, in which case I don't want to validate yet. I managed to turn off Client Side validation by giving my 'Upload Image' submit button an a class value of "style-name cancel" , so <input type="submit" name="UploadImageButton" value="Upload Image" class="style-name cancel" /> Now, when I post back, my model has a property

ASP.NET MVC How to convert ModelState errors to json

こ雲淡風輕ζ 提交于 2019-11-27 16:40:21
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 LINQ? The .ErrorMessage property is so far down the chain that I don't know how to write the LINQ...

Updating value provider prior to TryUpdateModel

女生的网名这么多〃 提交于 2019-11-27 07:56:09
问题 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

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

Testing ModelState is always valid in asp.net mvc

谁说我不能喝 提交于 2019-11-27 02:33:38
问题 When testing my controller's actions the ModelState is always valid. public class Product { public int Id { get; set; } [Required] [StringLength(10)] public string Name { get; set; } [Required] public string Description { get; set; } [Required] public decimal Price { get; set; } } And my controller. public class ProductController : Controller { [HttpPost] public ActionResult Create(Product product) { if (ModelState.IsValid) { // Do some creating logic... return RedirectToAction("Display"); }

Is there a strongly-named way to remove ModelState errors in ASP.NET MVC

你。 提交于 2019-11-26 21:33:13
问题 Is there a way to remove ModelState errors during an ASP.NET MVC postback without having to write each one by hand. Let's say we have a checkbox Billing Same As Shipping and we want to then ignore anything user wrote for ShippingAddress when it's checked - typically what you might do is this. ModelState.Remove("Checkout.ShipppingAddress.FirstName"); ModelState.Remove("Checkout.ShipppingAddress.LastName"); ModelState.Remove("Checkout.ShipppingAddress.Address1"); ModelState.Remove("Checkout