Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

WebApi MVC 6 Model State Methods missing ex: ModelState.IsValid and AddModelError #3315

Closed
snapbatch opened this issue Oct 13, 2015 · 9 comments
Labels

Comments

@snapbatch
Copy link

I am converting a ASP 4 Web Api 2 to the new Asp.Net 5. After removing the old System.Web.Http and System.Web.Http.Description and replacing those NameSpaces with Microsoft.AspNet.Mvc. Now on a simple route in my controller. I am getting an error ModelState does not contain a definition for IsValid.

if (!ModelState.IsValid)
return new HttpStatusCodeResult(305);

This also happens in ModelState.AddModelError

@snapbatch
Copy link
Author

@Eilon I should have mention that I do have the dependency Microsoft.AspNet.Mvc.ModelBinding; but its still giving me the error.

My class consist of and none of these are working.
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.ModelBinding.Validation;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Routing;

@pranavkm
Copy link
Contributor

Is your controller type deriving from Microsoft.Aspnet.Mvc.Controller?

@Eilon
Copy link
Member

Eilon commented Oct 14, 2015

Hover over the ModelState property and press F12 to go to its definition. I'm guessing it's the wrong type to begin with.

@Eilon
Copy link
Member

Eilon commented Oct 14, 2015

Also, you shouldn't have to import any namespaces to access that. If you're in a controller, you have access to it.

@snapbatch
Copy link
Author

@pranavkm I thought that in MVC 6 there is no need for base controller classes. I received that information from a video here. https://channel9.msdn.com/Events/dotnetConf/2014/MVC-6

@Eilon I went into the definition. ModelState is using the Controller class in the namespace Microsoft.AspNet.Mvc

@snapbatch
Copy link
Author

Ok Thanks @Eilon and @pranavkm I had to add Microsoft.AspNet.Mvc.Controller as a base controller. If I were you guys. I would really look at taking that video down because it clearly states that in MVC6 a base class is not required. If it is required and I just misheard it wrong. The video needs to be more clear at explaining the "why".

Thanks!

@Muchiachio
Copy link
Contributor

It's not required, but if you want to use ModelState which is a property of Microsoft.AspNet.Mvc.Controller, you have to extend it.
You can't access ModelState from thin air right? 😃

@NeelBhatt
Copy link

@Muchiachio I guess ModelState is not a property but an instance of class ModelStateDictionary.cs which has IsValid propery.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants