OK, I'm close here. I have a simple MVC3 application with viewmodel
ViewModel
public class TicketViewModel {
public Ticket Ticket { get; set; }
[DisplayName("Name")]
[Required(ErrorMessage = "Requestor name is required.")]
public string Name { get; set; } }
controller
[HttpPost]
public ActionResult Create(TicketViewModel vm)
{
if(ModelState.IsValid) {
TempData["message"] = "Your ticket has been submitted.";
TempData["message-class"] = "success";
return RedirectToAction("Index");
}
TempData["message-class"] = "error";
return View("Index", vm);
}
For some reason, ModelState.IsValid passes all the time as true . Even when the name is left blank. It, as a model / viewmodel, does not check at all. This works in other applications, so I'm sure I'm not connecting something. I have all the javascript that the validation is included in, although I don't think the problem is right now.
Update
Interestingly, the html tags that are generated by @ Html.TextBoxFor () do not include the data-val and data-val attributes.
View
@model MyApp.ViewModels.TicketViewModel
@{
ViewBag.Title = "Tickets";
}
<div id="main-content">
<section class="large">
<div class="section">
<div class="section-header">Submit Ticket</div>
<div class="section-content">
<div class="message"></div>
@using( Html.BeginForm("Create", "Home", FormMethod.Post) ) {
<h2>User Information</h2>
<dl>
<dt>@Html.LabelFor( m => m.Name)</dt>
<dd>
@Html.TextBoxFor( m => m.Name)
@Html.ValidationMessageFor( m => m.Name)
</dd>
<dt></dt>
<dd><button>Submit</button></dd>
</dl>
}
</div>
</div>
</section>
</div>
UPDATE II
, . . , DI global.asax.cs, . ,
public void SetupDependencyInjection() {
_kernel = new StandardKernel();
RegisterServices(_kernel);
DependencyResolver.SetResolver(new NinjectDependencyResolver(_kernel));
}
Application_Start()
protected void Application_Start()
{
SetupDependencyInjection();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
SetupDependencyInjection(), . , DI , , , . MVC3.