title: MVC 4 Strongly Typed Views Not Passing Model For Controller
body: <p>Not long ago I built a maintenance request feature in my current MVC 4 project.  It worked out perfectly so when I needed another similar tooling request feature added I followed the same composition just with slightly modified tables and model properties etc.  Now for some reason within the tooling request controller none of the model binding seems to work (each httppost method receives a null value for the model being passed).</p>
  
  <p>For the feature I've created 3 models.  Essentially one holds a list of all the requests, one is the model for each request itself with a list of notes associated with it and then the third is the note model.</p>
  
  <p>Models:</p>
  
  <pre><code>    namespace TeamPortal.Models
  {
      public class ToolingRequestModels
      {
          public List&lt;ToolingRequestModel&gt; ToolingRequests { get; set; }
          public SelectList Sources { get; set; }
          public SelectList Assignees { get; set; }
          public SelectList DefectTypes { get; set; }
  
          public ToolingRequestModels()
          {
              TeamPortalService TPsvc = new TeamPortalService();
  
              Sources = new SelectList(TPsvc.GetToolingRequestSources(), "Text", "Value");
              Assignees = new SelectList(TPsvc.GetToolingRequestAssignees(), "Text", "Value");
              DefectTypes = new SelectList(TPsvc.GetToolingRequestDefectTypes(), "Text", "Value");
          }
  
          public ToolingRequestModels(string ListType)
          {
              ToolingRequestModels baseModel = new ToolingRequestModels();
  
              TeamPortalService TPsvc = new TeamPortalService();
  
              List&lt;int&gt; RequestIDList = TPsvc.GetToolingRequests(ListType);
  
              List&lt;ToolingRequestModel&gt; tempToolingRequests = new List&lt;ToolingRequestModel&gt;();
              foreach (int requestID in RequestIDList)
              {
                  ToolingRequestModel tempToolingRequestModel = new ToolingRequestModel(requestID, baseModel.Sources, baseModel.Assignees, baseModel.DefectTypes);
                  tempToolingRequests.Add(tempToolingRequestModel);
              }
              ToolingRequests = tempToolingRequests;
          }
      }
  
      public class ToolingRequestModel
      {
          public int RequestID { get; set; }
          public string Requester { get; set; }
          public DateTime RequestDate { get; set; }
          public string Summary { get; set; }
  
          public SelectList Sources { get; set; }
          public string Source { get; set; }
  
          public string Description { get; set; }
  
          public SelectList Assignees { get; set; }
          public string AssignedTo { get; set; }
  
          public SelectList DefectTypes { get; set; }
          public string DefectType { get; set; }
  
          public string Status { get; set; }
          public string Model { get; set; }
          public DateTime? CompletionDate { get; set; }
          public bool CloseRequest { get; set; }
  
          public string AddNote { get; set; }
          public List&lt;ToolingNoteModel&gt; Notes { get; set; }
  
          public ToolingRequestModel()
          {
              TeamPortalService TPsvc = new TeamPortalService();
  
              Sources = new SelectList(TPsvc.GetToolingRequestSources(), "Text", "Value");
              Assignees = new SelectList(TPsvc.GetToolingRequestAssignees(), "Text", "Value");
              DefectTypes = new SelectList(TPsvc.GetToolingRequestDefectTypes(), "Text", "Value");
          }
  
          public ToolingRequestModel(int requestID, SelectList slSources, SelectList slAssignees, SelectList slDefectTypes)
          {
              TeamPortalService TPsvc = new TeamPortalService();
  
              ToolingRequestModel modelDetails = TPsvc.GetToolingRequestDetails(requestID);
  
              RequestID = requestID;
              Requester = modelDetails.Requester;
              RequestDate = modelDetails.RequestDate;
              Summary = modelDetails.Summary;
              Sources = slSources;
              Source = modelDetails.Source;
              Description = modelDetails.Description;
              Assignees = slAssignees;
              AssignedTo = modelDetails.AssignedTo;
              DefectTypes = slDefectTypes;
              DefectType = modelDetails.DefectType;
              Status = modelDetails.Status;
              Model = modelDetails.Model;
              CompletionDate = modelDetails.CompletionDate;
  
              Notes = TPsvc.GetToolingRequestNotes(requestID);
  
  
          }
      }
  
      public class ToolingNoteModel
      {
          public int ToolingNoteID { get; set; }
          public int RequestID { get; set; }
          public string NoteText { get; set; }
          public string NoteAuthor { get; set; }
          public DateTime NoteCreateDate { get; set; }
      }
  }
  </code></pre>
  
  <p>To simplify things we can just look at the controller and view for adding a new request, however the model binding is also failing when trying to update requests.  Fairly straight forward input form, it does have a check to verify we have records of the model input and no records are found the get controller method is recalled with an error message.</p>
  
  <p>Controller:</p>
  
  <pre><code>        [ValidateInput(false)]
      public virtual ActionResult ToolingRequestAdd(bool? requestAdd, bool? badModel)
      {
          ToolingRequestModel model = new ToolingRequestModel();
  
          if (requestAdd == true)
          {
              ViewBag.RequestAdd = true;
          }
          else
          {
              ViewBag.RequestAdd = false;
          }
  
          if (badModel == true)
          {
              ViewBag.BadModel = true;
          }
          else
          {
              ViewBag.BadModel = false;
          }
  
          return View(model);
      }
  
      [HttpPost]
      [ValidateInput(false)]
      public virtual ActionResult ToolingRequestAdd(ToolingRequestModel model)
      {
          TeamPortalService TPsvc = new TeamPortalService();
          GP10Service gp10svc = new GP10Service();
  
          model.Requester = User.Identity.Name;
          model.RequestDate = DateTime.Now;
          model.Status = "New";
  
          if (gp10svc.CheckModelCode(model.Model))
          {
              return RedirectToAction("ToolingRequestAdd", new
              {
                  requestAdd = false,
                  badModel = true
              });
          }
  
          TPsvc.SaveToolingRequest(model);
  
          return RedirectToAction("ToolingRequestAdd", new
          {
              requestAdd = true
          });
      }
  </code></pre>
  
  <p>Here is the view:</p>
  
  <pre><code>    &lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/FixedColumn.Master" Inherits="System.Web.Mvc.ViewPage&lt;TeamPortal.Models.ToolingRequestModel&gt;" %&gt;
  
  &lt;asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server"&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content4" ContentPlaceHolderID="LeftContent" runat="server"&gt;
          &lt;% Html.RenderPartial("LocalNavToolingRequests"); %&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="RightContent" runat="server"&gt;
  
  &lt;div class="box"&gt;
          &lt;!-- box / title --&gt;
          &lt;div class="title"&gt;
              &lt;h5&gt;
                  Add Tooling Request&lt;/h5&gt;
          &lt;/div&gt;
          &lt;%if (ViewBag.RequestAdd)
            { %&gt;
                &lt;div class="messages"&gt;&lt;div id="successes0" class="message message-success"&gt;&lt;div class="image"&gt;&lt;img height="32" src="/Content/images/icons/success.png"&gt;&lt;/div&gt;&lt;div class="text"&gt;&lt;h6&gt;Success&lt;/h6&gt;&lt;span&gt;Tooling Request Saved Successfully.&lt;/span&gt;&lt;/div&gt;&lt;div class="dismiss"&gt;&lt;a href="#successes0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
          &lt;%  } %&gt;
          &lt;%if (ViewBag.BadModel)
            { %&gt;
                &lt;div class="messages"&gt;&lt;div id="errors0" class="message message-error"&gt;&lt;div class="image"&gt;&lt;img height="32" src="/Content/images/icons/error.png"&gt;&lt;/div&gt;&lt;div class="text"&gt;&lt;h6&gt;Error&lt;/h6&gt;&lt;span&gt;The Spa Model Code Entered Was Invalid.  Verify The Model Is Still Active And The 3 Digits Are Correct.&lt;/span&gt;&lt;/div&gt;&lt;div class="dismiss"&gt;&lt;a href="#errors0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
          &lt;%  } %&gt;
  
          &lt;% using (Html.BeginForm())
             { %&gt;
  
             &lt;div class="form"&gt;
              &lt;div class="fields"&gt;
  
              &lt;div class="field  field-first"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Request Summary:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="textbox"&gt;
                      &lt;%: Html.TextBoxFor(m=&gt;m.Summary) %&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="field"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Model:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="textbox"&gt;
                      &lt;%: Html.TextBoxFor(m=&gt;m.Model) %&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="field"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Source:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="select"&gt;
                      &lt;%: Html.DropDownListFor(m =&gt; m.Source, Model.Sources)%&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="field"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Defect Type:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="select"&gt;
                      &lt;%: Html.DropDownListFor(m =&gt; m.DefectType, Model.DefectTypes)%&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="field"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Description:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="textarea textarea-editor"&gt;
                      &lt;%: Html.TextAreaFor(m =&gt; m.Description, 12, 70, new { @class = "tinymce-simple" })%&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;% if (HttpContext.Current.User.IsInRole("Tooling"))
                 { %&gt;
              &lt;div class="field"&gt;
                  &lt;div class="label"&gt;
                      &lt;label&gt;Assign Request To:&lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="select"&gt;
                      &lt;%: Html.DropDownListFor(m =&gt; m.AssignedTo, Model.Assignees)%&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;%} %&gt;
  
              &lt;/div&gt;
             &lt;/div&gt;
             &lt;input id="submit" name="submit" type="submit" value="Submit Request" /&gt;
         &lt;%} %&gt;
  
  &lt;/div&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>As a workaround I can use FormCollection to recreate a new model and fill the needed properties before saving or redirecting, however I'd really like to know where I've gone wrong here (plus FormCollection is a pain).</p>
  
  <p>Very strange that this fails where the maintenance requests work fine.  I thought copying my previous process would save me time...</p>
  
  <p>Hopefully this question isn't "too localized", seems like it might be depending on the solution, I can re-post this elsewhere if that's the case.</p>
  
  <p>Any help would be great, let me know if I've included enough (or too much) code/information.  Thanks!</p>