title: Display SQL View as HTML table with SelectList default value
body: <p>I'm displaying an SQL Server view <em>vw_FormattedData</em> in a HTML table. I am trying to update the column <em>superUserLevel</em> with a value based on user input. (A HTML Select input)</p>
  
  <p>Basically I am trying to set the selected value of the HTML selectbox (SelectList) to whatever is in my View.</p>
  
  <p>Is there a way in my Index.chtml loop to pass @item.superUserLevel into this? I may be going about this in completely the wrong way. Any help is much appreciated.</p>
  
  <p><img src="https://i.stack.imgur.com/SHGBX.png" alt="enter image description here"></p>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>namespace FormPostTest.Controllers
  {
      public class HomeController : Controller
      {
  
          private DannyContext db = new DannyContext();
  
          public ActionResult Index()
          {
              ViewData["OurData"] = db.vw_FormattedData.ToList();
  
              var queryLevels = from a in db.Level select a;
              ViewData["Levels"] = new SelectList(queryLevels, "id", "levelDescription", "DONT KNOW WHICH IS SELECTED FROM HERE");
  
              return View();
          }
      }
  }
  </code></pre>
  
  <p><strong>Index.chtml</strong></p>
  
  <pre><code>@model FormPostTest.Models.vw_FormattedData
  
  @{
      ViewBag.Title = "Home Page";
  }
  
  &lt;div class="row top-buffer"&gt;
      &lt;div class="col-md-12"&gt;
          &lt;table class="table table-striped top-buffer"&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      Forename
                  &lt;/th&gt;
                  &lt;th&gt;
                      Surname
                  &lt;/th&gt;
                  &lt;th&gt;
                      Line1
                  &lt;/th&gt;
                  &lt;th&gt;
                      Line2
                  &lt;/th&gt;
                  &lt;th&gt;
                      City
                  &lt;/th&gt;
                  &lt;th&gt;
                      Country
                  &lt;/th&gt;
                  &lt;th&gt;
                      Super User Level
                  &lt;/th&gt;
              &lt;/tr&gt;
  
              @foreach (var item in @ViewData["OurData"] as List&lt;FormPostTest.Models.vw_FormattedData&gt;)
              {
                  &lt;tr&gt;
                      &lt;td&gt;
                          @item.forename
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.surname
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.line1
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.line2
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.city
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.country
                      &lt;/td&gt;
                      &lt;td&gt;
                          @Html.DropDownList("Levels", null, "** Please Select **", new { @class = "form-control" })
                      &lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>create table person(
    id int not null identity(1,1) primary key,
    forename varchar(50) not null,
    surname varchar(50) not null
  );
  
  insert into person (forename, surname) values ('Arsene', 'Wenger');
  insert into person (forename, surname) values ('Nigel', 'Pearson');
  
  create table address(
    id int not null identity(1, 1) primary key,
    line1 varchar(50),
    line2 varchar(50),
    city varchar(50),
    country varchar(50),
    personId int not null
  );
  
  insert into address (line1, line2, city, country, personId) values ('Emirates Stadium', '75 Drayton Park', 'London', 'England', 1);
  insert into address (line1, line2, city, country, personId) values ('King Power Stadium', 'Filbert Way', 'Leicester', 'England', 2);
  
  create table superuser(
    id int not null identity(1, 1) primary key,
    personId int not null,
    permissionLevel int
  );
  
  create view vw_FormattedData as
  select
    p.forename,
    p.surname,
    a.line1,
    a.line2,
    a.city,
    a.country,
    su.permissionLevel as superUserLevel
  from
    person p
  left join
    address a
    on a.personId = p.id
  left join
    superuser su
    on su.personId = p.id;
  
  create table levels(
    id int not null primary key,
    levelDescription varchar(50)
  );
  
  insert into levels (id, levelDescription) values (1, 'One');
  insert into levels (id, levelDescription) values (2, 'Two');
  insert into levels (id, levelDescription) values (3, 'Three');
  </code></pre>