title: Debugging code in a view (asp.net mvc2)
body: <p>How do I debug code in the View in asp.net mvc2 application?</p>
  
  <p><strong>Edit after progress last night</strong>:</p>
  
  <p>Ok so now I have the following:</p>
  
  <p>in <code>Shared\EditorTemplates\Equipment.ascx</code> :</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;DAT.Models.Item&gt;" %&gt;
  
      &lt;% using (Html.BeginForm()) {%&gt;
          &lt;%: Html.ValidationSummary(true) %&gt;
  
          &lt;div class="editor-label"&gt;
              &lt;%: Html.Label("Item ID") %&gt;
              &lt;%: Html.TextBoxFor(model =&gt; model.ItemID) %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.ItemID) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              &lt;%: Html.LabelFor(model =&gt; model.ModelID) %&gt;
              &lt;%: Html.DropDownListFor(x =&gt; x.Model.Model1, new SelectList(Model.Model.Model1, "ModelId", "Model", Model.ModelID)) %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.ModelID) %&gt;
          &lt;/div&gt;
  
          ...
  </code></pre>
  
  <p>in <code>Item\Edit.aspx</code> :</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;DAT.ViewModels.ItemEditViewModel&gt;" %&gt;
  
      &lt;% using (Html.BeginForm()) {%&gt;
              &lt;%: Html.ValidationSummary(true) %&gt;
  
              &lt;fieldset&gt;
                  &lt;legend&gt;Fields&lt;/legend&gt;
                      &lt;%: Html.EditorFor(model =&gt; model.Item) %&gt;
                  &lt;p&gt;
                      &lt;input type="submit" value="Save" /&gt;
                  &lt;/p&gt;
              &lt;/fieldset&gt;
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>    public ActionResult Edit(int id)
      {
          var models = from p in database.Models.Where(x =&gt; x.Model1 != null) select p;
  
          var viewModel = new ViewModel
                              {
                                  Things = database.Things.Single(a =&gt; a.ItemID == id),
  
                                  //tried this:
                                  //Models = database.Models                 
  
                                  Models = models
                              };
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>So I am sure that the problem is with this line</p>
  
  <pre><code>&lt;%: Html.DropDownListFor(x =&gt; x.Model.Model1, new SelectList(Model.Model.Model1, "ModelId", "Model", Model.ModelID)) %&gt;
  </code></pre>
  
  <p>When generating the selectlist, I don't have IEnumerable for the first parameter? Or one of the values I am feeding this is causing null. How do I get the list of models in my view?</p>
  
  <p><strong>EDIT AFTER PULLING ALL MY HAIR OUT:</strong></p>
  
  <p>It seems the problem lies in this example: <a href="http://www.asp.net/mvc/tutorials/mvc-music-store-part-4" rel="nofollow noreferrer">http://www.asp.net/mvc/tutorials/mvc-music-store-part-4</a> . Strangely, I am not sure if it is following best practice. Look at the code and how they pass the models about - it seems stupidly obscure using ViewData["Blah"] and the Models as well, why can't you just have it all sent as the model? Look at the code how they have done it:</p>
  
  <p><strong>Album.ascx :</strong></p>
  
  <pre><code>&lt;%@ Import Namespace="MvcMusicStore"%&gt;
  
  &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;MvcMusicStore.Models.Album&gt;" %&gt;
  
  &lt;script src="/Scripts/MicrosoftAjax.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Title)%&gt;
      &lt;%: Html.TextBoxFor(model =&gt; model.Title)%&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.Title)%&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Price)%&gt;
      &lt;%: Html.TextBoxFor(model =&gt; model.Price)%&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.Price)%&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.AlbumArtUrl)%&gt;
      &lt;%: Html.TextBoxFor(model =&gt; model.AlbumArtUrl)%&gt;
      &lt;%: Html.ValidationMessageFor(model =&gt; model.AlbumArtUrl)%&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Artist)%&gt;
      &lt;%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;%: Html.LabelFor(model =&gt; model.Genre)%&gt;
      &lt;%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MvcMusicStore.ViewModels.StoreManagerViewModel&gt;" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt;
      Edit - &lt;%: Model.Album.Title %&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
      &lt;h2&gt;Edit Album&lt;/h2&gt;
  
      &lt;% Html.EnableClientValidation(); %&gt;
  
      &lt;% using (Html.BeginForm()) {%&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Edit Album&lt;/legend&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Album, new { Artists = Model.Artists, Genres = Model.Genres}) %&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  
      &lt;% } %&gt;
  
      &lt;div&gt;
          &lt;%:Html.ActionLink("Back to Albums", "Index") %&gt;
      &lt;/div&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p><strong>View Model:</strong></p>
  
  <pre><code>using System.Collections.Generic;
  using MvcMusicStore.Models;
  
  namespace MvcMusicStore.ViewModels
  {
      public class StoreManagerViewModel
      {
          public Album Album { get; set; }
          public List&lt;Artist&gt; Artists { get; set; }
          public List&lt;Genre&gt; Genres { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>And the controller:</strong></p>
  
  <pre><code>//
  // GET: /StoreManager/Edit/5
  
  public ActionResult Edit(int id)
  {
      var viewModel = new StoreManagerViewModel
      {
          Album = storeDB.Albums.Single(a =&gt; a.AlbumId == id),
          Genres = storeDB.Genres.ToList(),
          Artists = storeDB.Artists.ToList()
      };
  
      return View(viewModel);
  }
  </code></pre>
  
  <p>So it looks to me like the model is built in the controller as makes logical sense to me. Then in the view they use this statement which causes my confusion:</p>
  
  <p><code>&lt;%: Html.EditorFor(model =&gt; model.Album, new { Artists = Model.Artists, Genres = Model.Genres}) %&gt;</code></p>
  
  <p>The model is being split/changed and now we send the other (Artists, Genres) as ViewData ??
  Can someone explain, and is this at all fitting with the entire design pattern?</p>