title: Add update and delete in same view with MVC3 and RAZOR
body: <p>I am new to MVC </p>
  
  <p>I am using Entity Framework.</p>
  
  <p>I have three database table Those are </p>
  
  <pre><code>User: UserID,UserName
  
  Role: RoleID,RoleName
  
  UserRoles: ID,UserID,RoleID
  </code></pre>
  
  <p><strong>UserDataModel.cs</strong></p>
  
  <pre><code> [MetadataTypeAttribute(typeof(UserMetaData))]
  public partial class User
  {
      [Required(ErrorMessage = "Please enter your again")]
      [DataType(DataType.Password)]
      [Compare("Password")]
      public String ConfirmPassword { get; set; }
  
  
  }
  
  public class UserMetaData
  {
      [Required(AllowEmptyStrings = false, ErrorMessage = "You forgot to enter your name.")]
      [Display(Name = "Your Name")]
      [StringLength(50, MinimumLength = 3, ErrorMessage = "Your name is too long. Please change your name if you can !")]
      public String UserName { get; set; }
  
      [Required(ErrorMessage = "Do you know your gender ?? if you don't know, leave it !!")]
      public Boolean Gender { get; set; }
  
      [Required(ErrorMessage = "You forgot to select your current country.")]
      public Int32 CountryID { get; set; }
  
      [Required(ErrorMessage = "Please tell us your 'Happy birth date'. We may brings you a huge gift for you on your birthday.")]
      [RegularExpression("^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$", ErrorMessage = "Oh Noo!! Please enter date in dd/MM/yyy formate")]
      public DateTime DOB { get; set; }
  
      [Required(ErrorMessage = "Please tell us your email address. we assure you that we can't reach you with your email address.")]
      [RegularExpression("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", ErrorMessage = "Oh Noo!! Please enter date in dd/MM/yyy formate")]
      public String Email { get; set; }
  
  
      [Required(ErrorMessage = "Please tell us your contact number. we assure that we wont give this number to anyone.")]
      public String ContactNo { get; set; }
  
      //[Required(ErrorMessage = "You have to select atleaset one role for you.")]
      //public List&lt;Roles&gt; Roles { get; set; }
  
      [Required(ErrorMessage = "Your security can not be compromised. Please enter your password")]
      [DataType(DataType.Password)]
      public String Password { get; set; }
  
  
  
  
  }
  </code></pre>
  
  <p><strong>RegistrationController.cs</strong></p>
  
  <pre><code> DbMVCEntities db = new DbMVCEntities();
  
    public ActionResult RegisterUser()
    {
          return View();
    }
  
    [HttpPost]
      public ActionResult RegisterUser(User _user, string[] Roles)
      {
          try
          {
              db.Users.AddObject(_user);
              db.SaveChanges();
  
              for (int i = 0; i &lt;= Roles.Length; i++)
              {
                  UserRole _UserRole = new UserRole();
                  _UserRole.UserID = _user.UserID;
                  _UserRole.RoleID = Convert.ToInt32(Roles[0]);
  
                  db.UserRoles.AddObject(_UserRole);
                  db.SaveChanges();
              }
          }
          catch
          {
  
          }
  
          return RedirectToAction("RegisterUser");
      }
  
  
      public ActionResult Edit(int id)
      {
          try
          {
              var User = db.Users.SingleOrDefault(x =&gt; x.UserID == id);
              if (User != null)
                  return View("RegisterUser", User);
              else
                  return RedirectToAction("RegisterUser");
          }
          catch (Exception ex)
          {
              throw;
          }
      }
  </code></pre>
  
  <p><strong>RegisterUser.cshtml</strong></p>
  
  <pre><code>@model MVCRegistration.User
  @{
  ViewBag.Title = "RegisterUser";
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
  @using (Html.BeginForm())
  {
  &lt;table width="100%"&gt;
      &lt;tr&gt;
          &lt;td width="15%"&gt;
              Name :
          &lt;/td&gt;
          &lt;td width="10%"&gt;
              @Html.TextBoxFor(x =&gt; x.UserName)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.UserName)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Your Name :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.RadioButtonFor(x =&gt; x.Gender, true)Male
              @Html.RadioButtonFor(x =&gt; x.Gender, false)Female
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.Gender)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Country :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DropDownListFor(x =&gt; x.CountryID, new SelectList((new MVCRegistration.DbMVCEntities()).Countries.ToList(), "CountryID", "CountryName"), "--Select--")
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.CountryID)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Birth Date :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x.DOB)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.DOB)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Email :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x.Email)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.Email)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Contact No. :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x.ContactNo)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.ContactNo)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td width="10%" valign="top"&gt;
              Roles :
          &lt;/td&gt;
          &lt;td style="text-transform: lowercase;"&gt;
              @foreach (MVCRegistration.Role role in new MVCRegistration.DbMVCEntities().Roles)
              {
                  &lt;input type="checkbox" name="Roles" value="@role.RoleID" /&gt; @role.RoleName &lt;br /&gt;
              }
          &lt;/td&gt;
          &lt;td width="80%"&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              Password :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.PasswordFor(x =&gt; x.Password)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.Password)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              System Password :
          &lt;/td&gt;
          &lt;td&gt;
              @Html.PasswordFor(x =&gt; x.ConfirmPassword)
          &lt;/td&gt;
          &lt;td width="80%"&gt;
              @Html.ValidationMessageFor(x =&gt; x.ConfirmPassword)
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td colspan="3"&gt;
              &lt;input type="submit" value="Register Me" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              Name
          &lt;/th&gt;
          &lt;th&gt;
              Gender
          &lt;/th&gt;
          &lt;th&gt;
              Birth Date
          &lt;/th&gt;
          &lt;th&gt;
              Country
          &lt;/th&gt;
          &lt;th&gt;
              Email
          &lt;/th&gt;
          &lt;th&gt;
              Contact No
          &lt;/th&gt;
          &lt;th&gt;
          &lt;/th&gt;
      &lt;/tr&gt;
      @foreach (MVCRegistration.User _user in new MVCRegistration.DbMVCEntities().Users)
      {
  
          &lt;tr&gt;
              &lt;td&gt;
                  @_user.UserName
              &lt;/td&gt;
              &lt;td&gt;
                  @if (@_user.Gender)
                  {
                      @Html.Raw("Male");
                  }
                  else
                  {
                      @Html.Raw("Female");
                  }
              &lt;/td&gt;
              &lt;td&gt;
                  @_user.DOB.ToString("dd/MM/yyyy")
              &lt;/td&gt;
              &lt;td&gt;
                  @_user.Country.CountryName
              &lt;/td&gt;
              &lt;td&gt;
                  @_user.Email
              &lt;/td&gt;
              &lt;td&gt;
                  @_user.ContactNo
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.ActionLink("Edit", "Edit", new { id = _user.UserID }) |
                  @Html.ActionLink("Details", "Details", new { id = _user.UserID }) |
                  @Html.ActionLink("Delete", "Delete", new { id = _user.UserID })
              &lt;/td&gt;
          &lt;/tr&gt;
  
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>Its working fine !!! when i click on edit all the data filled up except user roles checkbox.</p>
  
  <p>Actually i am having roles list in "UserRoles".</p>
  
  <p>How to get list of user roles in edit mode???????</p>
  
  <p>Please help me out. If i am doing wrong anything please let me know.</p>