title: How To Create Razor MVC That Accesses Data from a Stored Procedure
body: <p>New to MVC, I am trying to get a model-view-component that works. The only constraints are that the data gets populated by a stored proc in a C# class, and that the PartialView uses Razor and is stored in the _Shared folder so all pages can reference it.</p>
  
  <p>All I have that I think might work is the ViewModel, BreadcrumbViewModel.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  
  namespace Web.Controllers.ViewModels
  {
    public class BreadcrumbViewModel
    {
      public int Id { get; set; }
      public int ParentId { get; set; }
      public string Label { get; set; }
      public string URL { get; set; }
      public int SortOrder { get; set; }
      public int InverseDepth { get; set; }
    }
  
    public class BreadcrumbsViewModel
    {
      public List&lt;BreadcrumbViewModel&gt; getBreadcrumbModel(string thisURL)
      {
          //thisURL contains the path of the current page, so the stored proc can find its parents.
          List&lt;BreadcrumbViewModel&gt; listBVM = new List&lt;BreadcrumbViewModel&gt;();
          BreadcrumbViewModel model;
          string connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDataContext"].ConnectionString;
          using (SqlConnection conn = new SqlConnection(connString))
          {
              conn.Open();
              using (SqlCommand cmd = new SqlCommand())
              {
                  cmd.Connection = conn;
                  cmd.CommandType = System.Data.CommandType.StoredProcedure;
                  cmd.CommandText = "getBreadcrumb";
                  cmd.Parameters.Add(new SqlParameter("@thisURL", SqlDbType.NVarChar));
                  cmd.Parameters["@thisURL"].Value = thisURL;
  
                  using (SqlDataReader reader = cmd.ExecuteReader())
                  {
                      while (reader.Read())
                      {
                          model = new BreadcrumbViewModel
                          {
                              Id = Convert.ToInt32(reader[0]),
                              ParentId = Convert.ToInt32(reader[1]),
                              Label = reader[2].ToString(),
                              URL = reader[3].ToString(),
                              SortOrder = Convert.ToInt32(reader[4]),
                              InverseDepth = Convert.ToInt32(reader[5])
                          };
                          listBVM.Add(model);
                      }
                  }
              }
          }
          if (listBVM == null)
          {
              model = new BreadcrumbViewModel
              {
                  Id = 1,
                  ParentId = 0,
                  Label = "Home",
                  URL = "/",
                  SortOrder = 1,
                  InverseDepth = 1
              };
              listBVM.Add(model);
          }
          return listBVM;
      }
    }
  }
  </code></pre>
  
  <p>If this ViewModel is correct, what would the Controller and PartialView that references the List&lt;> look like?</p>