title: MVC 3 Get result from partial view into model
body: <p>I'm sure this is easy, but maybe I haven't searched well ...</p>
  
  <p>I want to know how to get results from a partial view back to the model and/or controller.</p>
  
  <p>If the user enters a FirstName, Gender (from drop down) and Grade (from drop down), I only find then FirstName and Gender in the model.  I want to know how to get the Grade from the drop down in the partial view all the way back into the model, so I can see it in the controller.</p>
  
  <p>Please look for this question in the controller code:</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <blockquote>
          <blockquote>
            <p>What do I need to do to get the GradeLevel from the partial class to be here: &lt;&lt;&lt;&lt;&lt;</p>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>Note: this is not the exact code. There may be small, insignificant typo's.</p>
  
  <p>EDIT: Apparently you can't add a long comment, so I will add here:</p>
  
  <p>Thank you, Tom and Mystere Man. Tom got me thinking as to why it doesn't work. I didn't think through the model binding. With the design I proposed, the HTML gets rendered and the Grade drop down has this id: "Grade".  The property on the model I want to bind to is: "GradeLevelID". If I change the helper in the partial view to be @Html.DropDownList("GradeLevelID" ... it works perfectly. </p>
  
  <p>But that is not a good solution. My idea was to abstract the partial view from the main view. Hard coding the name blows that!  I did work up a slightly improved solution.  In the main view, I change the @Html.Partial statement to pass the model property name to the partial. Like such:</p>
  
  <pre><code>@Html.Partial("GradeDropDown", (SelectList)Model.GradeSelectList, new ViewDataDictionary { { "modelPropertyName", "GradeLevelID" } })
  </code></pre>
  
  <p>Then I could change the partial view to say </p>
  
  <pre><code>@model System.Web.Mvc.SelectList
  @Html.DropDownList((string)ViewData["modelPropertyName"], Model)
  </code></pre>
  
  <p>But that also seems like a goofy way to approach things.  Thanks for the help. I'll look at EditorTemplates.</p>
  
  <hr>
  
  <h2>Here is my model:</h2>
  
  <pre><code>public class RegisterModel{
  
      public MemberRegistration MemberRegistration{
          get{
              if (HttpContext.Current.Session["MemberRegistration"] == null){
                  return null;
              }
              return (MemberRegistration)HttpContext.Current.Session["MemberRegistration"];
  
          }
  
          set{
              HttpContext.Current.Session["MemberRegistration"] = value;
          }
  
      }
  
      public string FirstName{
          get{
              return MemberRegistration.FirstName;
          }
          set{
              MemberRegistration.FirstName = value;
          }
      }
  
      public SelectList GenderSelectList{
          get{
  
              List&lt;object&gt; tempList = new List&lt;object&gt;();
  
              tempList.Add(new { Value = "", Text = "" });
              tempList.Add(new { Value = "M", Text = "Male" });
              tempList.Add(new { Value = "F", Text = "Female" });
  
              return new SelectList(tempList, "value", "text", MemberRegistration.Gender);
          }
      }
  
      [Required(ErrorMessage = "Gender is required")]
      public string Gender{
          get{
              return MemberRegistration.MemberPerson.Gender;
          }
          set{
              MemberRegistration.MemberPerson.Gender = value;
          }
      }
  
      public SelectList GradeLevelSelectList{
          get{
              List&lt;object&gt; tempList = new List&lt;object&gt;();
  
              tempList.Add(new { Value = "", Text = "" });
              tempList.Add(new { Value = "1", Text = "1st" });
              tempList.Add(new { Value = "2", Text = "2nd" });
              tempList.Add(new { Value = "3", Text = "3rd" });
              tempList.Add(new { Value = "4", Text = "4th" });
  
              return new SelectList(tempList, "value", "text", MemberRegistration.GradeLevel);
          }
      }
  
      [Required(ErrorMessage = "Grade is required")]
      public Int32 GradeLevel{
          get{
              return MemberRegistration.GradeLevel;
          }
          set{
              MemberRegistration.GradeLevel = value;
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Here is my main view:</h2>
  
  <pre><code>@model RegisterModel
  
  @using (Html.BeginForm())
  {
     &lt;p class="DataPrompt"&gt;
          &lt;span class="BasicLabel"&gt;First Name:&lt;/span&gt;
          &lt;br /&gt;
          @Html.EditorFor(x =&gt; x.FirstName)
      &lt;/p&gt;
  
      &lt;p class="DataPrompt"&gt;
          &lt;span class="BasicLabel"&gt;Gender:&lt;/span&gt;
          &lt;br /&gt;
          @Html.DropDownListFor(x =&gt; x.Gender, Model.GenderSelectList)
      &lt;/p&gt;
  
      &lt;p class="DataPrompt"&gt;
          &lt;span class="BasicLabel"&gt;Grade:&lt;/span&gt;&lt;span class="Required"&gt;*&lt;/span&gt;
          &lt;br /&gt;
          @Html.Partial("GradeDropDown", (SelectList)Model.GradeLevelSelectList)
      &lt;/p&gt;
  
      &lt;p class="DataPrompt"&gt;
          &lt;input type="submit" name="button" value="Next" /&gt;
      &lt;/p&gt;
  
  }
  </code></pre>
  
  <hr>
  
  <h2>Here is my partial view (named "GradeDropDown"):</h2>
  
  <pre><code>@model System.Web.Mvc.SelectList
  
  @Html.DropDownList("Grade", Model)
  </code></pre>
  
  <hr>
  
  <h2>Here is my controller:</h2>
  
  <pre><code>[HttpPost]
  public ActionResult PlayerInfo(RegisterModel model)
  {
      string FirstName = model.Registration.FirstName;
      string Gender = model.Registration.Gender;
  
      &gt;&gt;&gt;&gt;&gt; What do I need to do to get the GradeLevel from the partial class to be here: &lt;&lt;&lt;&lt;&lt;
      Int32 GradeLevel = model.Registration.GradeLevel;           
  
      return RedirectToAction("Waivers");
  }
  </code></pre>