Search code examples
validationasp.net-mvc-3ninject-2

Model.IsValid always returning true


Ok I am near wits end here. I've got a simple MVC3 application with a viewmodel

ViewModel

    public class TicketViewModel {
    public Ticket Ticket { get; set; }

    [DisplayName("Name")]
    [Required(ErrorMessage = "Requestor's 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 is coming through as true all the time. Even when Name is left blank. It's like the model/viewmodel isn't validating at all. This works on other applications so I'm pretty sure I'm not hooking up something. I've got all the validation javascript included as well though I don't think that's the problem right now.

Update Interestingly enough, the html tags that are being generated by @Html.TextBoxFor() are NOT including the data-val and data-val-required 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

Well now this is interesting. I created a fresh app and got things working with basic code. Then when I added DI code to the global.asax.cs validations stopped working. Specifically, when I add

    public void SetupDependencyInjection() {
        _kernel = new StandardKernel();
        RegisterServices(_kernel);
        DependencyResolver.SetResolver(new NinjectDependencyResolver(_kernel));
    }

and call it from Application_Start()

    protected void Application_Start()
    {
        SetupDependencyInjection();

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

if I remove SetupDependencyInjection() validations start working. To be clear, DI works well but it seems to kill validations. This worked well prior to MVC3 Tools Update.


Solution

  • I was able to find a solution. Seems that when you install Ninject via nuget the configuration is a little different. It configures your application from the App_Start folder. Basically I was doubling up on my Ninject-Fu calling in from global.asax. This ended up causing the weird validation issues, though the other parts of the application were working.

    Ninject - Setting up an MVC3 application