title: How to set the default value for Html.DropDownListFor in MVC
body: <p>i have following code :
  <strong>controller method</strong>:</p>
  
  <pre><code> public ActionResult Register(int? registrationTypeId)
          {
              IEnumerable&lt;AccountType&gt; accountTypes = new List&lt;AccountType&gt;
              {
                  new AccountType
                  {
                      AccountTypeId = 1,
                      AccountTypeName = "Red"
                  },
                  new AccountType
                  {
                      AccountTypeId = 2,
                      AccountTypeName = "Blue"
                  }
              };
             // I want to select account type on registrationTypeId
              ViewBag.AccountTypes = accountTypes;
              return View();
        }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;div class="col-md-10"&gt;
              @Html.DropDownListFor(n =&gt; n.AccountType,
           new SelectList(ViewBag.AccountTypes, "AccountTypeId", "AccountTypeName"), new { @class = "form-control" })
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class RegisterViewModel
      { 
          [Required]
          [Display(Name = "Account Type")]
          public int AccountType { get; set; 
      }
  </code></pre>
  
  <p>As you can see <strong>registrationTypeId</strong> in controller , i want to set the type on its bases if it is not <strong>null</strong> ,otherwise set to red. I have tried a alot but nothing worked for me. Any help will be appreciated !</p>