2014-01-07 66 views
0

我是新來的MVC添加更新,並與MVC3和RAZOR

我使用實體框架相同的視圖中刪除。

我有三個數據庫表這些都是

User: UserID,UserName 

Role: RoleID,RoleName 

UserRoles: ID,UserID,RoleID 

UserDataModel.cs

[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<Roles> Roles { get; set; } 

    [Required(ErrorMessage = "Your security can not be compromised. Please enter your password")] 
    [DataType(DataType.Password)] 
    public String Password { get; set; } 




} 

RegistrationController.cs

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 <= 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 => x.UserID == id); 
      if (User != null) 
       return View("RegisterUser", User); 
      else 
       return RedirectToAction("RegisterUser"); 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 
    } 

RegisterUser.cshtml

@model MVCRegistration.User 
@{ 
ViewBag.Title = "RegisterUser"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
@using (Html.BeginForm()) 
{ 
<table width="100%"> 
    <tr> 
     <td width="15%"> 
      Name : 
     </td> 
     <td width="10%"> 
      @Html.TextBoxFor(x => x.UserName) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.UserName) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Your Name : 
     </td> 
     <td> 
      @Html.RadioButtonFor(x => x.Gender, true)Male 
      @Html.RadioButtonFor(x => x.Gender, false)Female 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.Gender) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Country : 
     </td> 
     <td> 
      @Html.DropDownListFor(x => x.CountryID, new SelectList((new MVCRegistration.DbMVCEntities()).Countries.ToList(), "CountryID", "CountryName"), "--Select--") 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.CountryID) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Birth Date : 
     </td> 
     <td> 
      @Html.TextBoxFor(x => x.DOB) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.DOB) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Email : 
     </td> 
     <td> 
      @Html.TextBoxFor(x => x.Email) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.Email) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Contact No. : 
     </td> 
     <td> 
      @Html.TextBoxFor(x => x.ContactNo) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.ContactNo) 
     </td> 
    </tr> 
    <tr> 
     <td width="10%" valign="top"> 
      Roles : 
     </td> 
     <td style="text-transform: lowercase;"> 
      @foreach (MVCRegistration.Role role in new MVCRegistration.DbMVCEntities().Roles) 
      { 
       <input type="checkbox" name="Roles" value="@role.RoleID" /> @role.RoleName <br /> 
      } 
     </td> 
     <td width="80%"> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Password : 
     </td> 
     <td> 
      @Html.PasswordFor(x => x.Password) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.Password) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      System Password : 
     </td> 
     <td> 
      @Html.PasswordFor(x => x.ConfirmPassword) 
     </td> 
     <td width="80%"> 
      @Html.ValidationMessageFor(x => x.ConfirmPassword) 
     </td> 
    </tr> 
    <tr> 
     <td colspan="3"> 
      <input type="submit" value="Register Me" /> 
     </td> 
    </tr> 
</table> 


<table> 
    <tr> 
     <th> 
      Name 
     </th> 
     <th> 
      Gender 
     </th> 
     <th> 
      Birth Date 
     </th> 
     <th> 
      Country 
     </th> 
     <th> 
      Email 
     </th> 
     <th> 
      Contact No 
     </th> 
     <th> 
     </th> 
    </tr> 
    @foreach (MVCRegistration.User _user in new MVCRegistration.DbMVCEntities().Users) 
    { 

     <tr> 
      <td> 
       @_user.UserName 
      </td> 
      <td> 
       @if (@_user.Gender) 
       { 
        @Html.Raw("Male"); 
       } 
       else 
       { 
        @Html.Raw("Female"); 
       } 
      </td> 
      <td> 
       @_user.DOB.ToString("dd/MM/yyyy") 
      </td> 
      <td> 
       @_user.Country.CountryName 
      </td> 
      <td> 
       @_user.Email 
      </td> 
      <td> 
       @_user.ContactNo 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = _user.UserID }) | 
       @Html.ActionLink("Details", "Details", new { id = _user.UserID }) | 
       @Html.ActionLink("Delete", "Delete", new { id = _user.UserID }) 
      </td> 
     </tr> 

    } 
</table> 

}

它的做工精細!當我點擊編輯除用戶角色複選框以外的所有數據。

其實我在「UserRoles」中有角色列表。

如何在編輯模式下獲取用戶角色列表???????

請幫我一把。如果我做錯了什麼,請讓我知道。

+0

你用什麼模型爲RegisterUser.cshtml()。通常你會使用@ Html.CheckBoxFor(model => model.SomeProperty)。 – acarlon

+0

請看看更新的問題...... –

+0

其實我使用foreach循環綁定了我的「MasterRole」複選框列表,而我的視圖是強類型的「用戶」。 –

回答

0

爲什麼要用

@foreach (MVCRegistration.Role role in new MVCRegistration.DbMVCEntities().Roles) 
      { 
       <input type="checkbox" name="Roles" value="@role.RoleID" /> @role.RoleName <br /> 
      } 

您可以使用

@for (var i = 0; i < MVCRegistration.DbMVCEntities().Roles; i++) 
{ 

@Html.CheckBoxFor(x => x.DbMVCEntities().Roles[i], new { @checked = "checked" }) 
@Html.LabelFor(x => x.DbMVCEntities().Roles[i]) 
} 

更改用戶類

[MetadataTypeAttribute(typeof(UserMetaData))] 
public partial class User 
{ 
    [Required(ErrorMessage = "Please enter your again")] 
    [DataType(DataType.Password)] 
    [Compare("Password")] 
    public String ConfirmPassword { get; set; } 
    public IEnumerable<Role> AllRoles { get; set; } 
     public UserRoleModel() 
{ 
    this.AllRoles = DbMVCEntities().Roles.Select(r => new Role 
    { 
     Name = r 
    }); 

} 
} 

然後ü可以考慮使用此AllRoles

+0

雅是它的真實,但是當我在創建模式中,我需要顯示「MasterRole」中的所有角色 –

+0

沒有得到你的要求 –

+0

在第一個空表格和網格與所有用戶在那裏。但是當我點擊從網格視圖編輯特定的行將充滿信息存儲在數據庫中。希望你有我的問題。 –

0

是否的提交你的 形式工作 - 是public ActionResult RegisterUser(User _user, string[] Roles)與您期望的數據一起被調用?

通常情況下,您的模型將是您提交的模型。所以,我會製作MVCRegistration.DbMVCEntities()。角色列出模型的成員。如Roles。您可以指定[NotMapped]屬性,以便EF不認爲它已映射到數據庫。然後你會這樣做:

 @for(int i = 0; i< Model.Roles.Count; i++) 
     { 
      @Html.CheckBoxFor(x => x.Roles[i]); 
     } 
+0

你的意思是說,在我的UserDataModel.cs我應該有一個更多的屬性,這是列表角色? –

+0

是的,除非我錯過了有關您的設計的東西。 – acarlon