modelstate

asp.net MVC ModelState is null in my Unit Test. Why?

ModelState is always returning null in my unit tests. I was hoping someone could tell me why. Given the following controller: public class TestController : Controller { public ViewResult Index() { return View(); } } My test gets null for ModelState with this test: public void ModelState_Is_Not_Null() { TestCon...

MVC 2 Data annotations problem

Hi. Going mad now. I have a MVC solution that i've upgraded from MVC 1 to 2. It all works fine.... except the Validation! Here's some code: In the controller: using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Security.Principal; using System.Web; using System.Web.Mvc; using S...

TDD - testing business rules/validation in ASP.NET MVC

Hi, I am using the sharp architecture so I can easily use mocks etc. in my unit tests and/or during TDD. I have quite complicated business rules and would like to test them at the controller level. I am just wondering how other people do this? For me validation tests business rules at three levels: (1) Property level (e.g. property is...

ASP.MVC 2 RTM + ModelState Error at Id property

I have this classes: public class GroupMetadata { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] public string Name { get; set; } } [MetadataType(typeof(GrupoMetadata))] public partial class Group { public virtual int Id { get; set; } public virtual string Name { get; set; } } And ...

How do I test ActionFilterAttributes that work with ModelState?

As suggested by (among others) Kazi Manzur Rashid in this blog post, I am using ActionFilterAttributes to transfer model state from one request to another when redirecting. However, I find myself unable to write a unit test that test the behavior of these attributes. As an example, this what I want the test for the ImportModelStateAttri...

ModelState always valid

I've got something seemingly very simple not working. I have got a model public class Name: Entity { [StringLength(10), Required] public virtual string Title { get; set; } } public class Customer: Entity { public virtual Name Name { get; set; } } a view model public class CustomerViweModel { public Customer Customer...

Error with Property Validation in Form Submission in ASP.NET MVC

I have a simple form on an ASP.NET MVC site that I'm building. This form is submitted, and then I validate that the form fields aren't null, empty, or improperly formatted. However, when I use ModelState.AddModelError() to indicate validation errors from my controller code, I get an error when my view is re-rendered. In Visual Studio, I...

ASP.NET MVC How to convert ModelState errors to json

How do you get a list of all ModelState error messages? I found this code to get all the keys: ( http://stackoverflow.com/questions/888521/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 me...

asp.net MVC ModelState.IsValid returning false

Hi, I am working in an ASP.NET MVC Application. I have a view model as follows: public class SampleInterestViewModel { //Properties defined //One such property that shows an error in ModelState is as follows public DateTime? SampleDate {get;set;} } From UI Perspective user can enter date as mmddyyyy. And when user enters in su...

Needing to copy properties before validation

I have a farily complex model needing to be validated, the problem is that this model is used on two different places, one where you register your customer and one where you simply add addresses. Some fields on the address are simply not visible on the register customer form. So when i check if ModelState.IsValid i get false of course ...

ASP.NET MVC model state as text

Is there an easy way to get a summary string of the errors that have been added to a controller's modelstate? I'm looking to return this in an Ajax method and want the validation errors etc to be returned to the client (i.e. the view does not exist for this method call). Or do I have to loop through the modelstate and look at each obj...

ASP.NET MVC: null reference exception using HtmlHelper.TextBox and custom model binder

I have written a class which implements IModelBinder (see below). This class handles a form which has 3 inputs each representing parts of a date value (day, month, year). I have also written a corresponding HtmlHelper extension method to print out three fields on the form. When the day, month, year inputs are given values which can be p...

Why is ModelStateToTempData Attribute from MVCContrib not working?

I'm simply trying to pass the ModelState from one action to another in the same controller, for validation purposes. However, the model state does not get updated. I see that TempData["__MvcContrib_ValidationFailures__"] contains the ModelStateDictionary from the forwarding Action, but I assumed this should get transfered into my current...

MVC 2 ModelState - Show all Errors

My asp.net form is throwing errors relating to an incorrect model, but on the page it's not showing me what model field threw the error. I'd like to Debug.WriteLine all errors in the ModelState but am unsure how to go about it. Any ideas? ...

.Net MVC2 How to add error to ModelState when using custom ValidationAttribute - Solved with IDataErrorInfo

I have the following ValidationAttribute class [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class DateValidationAttribute : ValidationAttribute { public DateValidationAttribute(string leftDateProperty, CompareOperator compareOperator, string rightDateProperty, string errorMessage) ...

ModelState reported as invalid for private properties on a model in ASP.NET MVC 2

I'm using ASP.NET MVC 2.0 and I am trying to take advantage of the model binding in my controller and also the modelstate validation. However I have come up against a problem and wanted to share it with people on here to see what you think. Ok I have my clean User poco in my model class library... namespace Model { public part...

How do I keep field values in the ASP.NET MVC form if it has been invalidated?

I have a form that is going through some validation before sending an e-mail. I have tried using this for validation, where the method ValidateInput sets the ModelState depending on the input: [HttpPost] public ActionResult Create(FormCollection collection) { ValidateInput(collection); if (ModelState.IsValid == false) return Vi...

ModelState doesnt contains Form values

i have this view model: public class ItemMenuViewModel { public ItemMenu Item { get; set; } public IEnumerable<SelectListItem> Pages { get; set; } } for this view: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SIIMVC.Controllers.ItemMenuViewModel>" %> <%@ Import Namespace="SIIMVC.Models.Ex...

Updating value provider prior to TryUpdateModel

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 : ) ...

How can I inject a ModelState with ninject?

Say I have CompaniesController which uses a CompaniesService. And the Companies Service is where I do all my Business Validation so it requires I pass it the ModelState (Im using a ModelState Wrapper following this... But since I added Ninject to my project I cant figure out how to inject the ModelStateWrapper into the CompaniesService.....