2014-09-13 18 views
0

我有一個表單,用戶可以在其中創建新員工。我想將userid存儲在提交表單的數據庫中。使用在MVC中提交表單的用戶標識更新數據庫

這是我創建新條目控制器:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Employee employee) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Employee.Add(employee); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(employee); 
    } 

我有兩個表。一個用於員工,另一個用於用戶。正如你可以看到我在我的員工表中有用戶ID,我想存儲提交表單的用戶ID。我能夠將表單值保存在我的員工表中,但無法獲取用戶ID。任何想法?

public class UsersContext : DbContext 
{ 
    public UsersContext() 
     : base("DefaultConnection") 
    { 
    } 

    public DbSet<UserProfile> UserProfiles { get; set; } 
    public DbSet<Employee> Employee { get; set; } 
} 

    [Table("UserProfile")] 
    public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
} 

[Table("Employee")] 
public class Employee 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int EmployeeID { get; set; } 
    public string EmployeeName { get; set; } 
    public int DepartmentID { get; set; } 
    public string Department { get; set; } 
    public int UserId { get; set; } 
} 

,這是我的登錄表單:

<section id="loginForm"> 
    <h2>Use a local account to log in.</h2> 
    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { 
@Html.AntiForgeryToken() 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>Log in Form</legend> 
    <ol> 
     <li> 
      @Html.LabelFor(m => m.UserName) 
      @Html.TextBoxFor(m => m.UserName) 
      @Html.ValidationMessageFor(m => m.UserName) 
     </li> 
     <li> 
      @Html.LabelFor(m => m.Password) 
      @Html.PasswordFor(m => m.Password) 
      @Html.ValidationMessageFor(m => m.Password) 
     </li> 
     <li> 
      @Html.CheckBoxFor(m => m.RememberMe) 
      @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" }) 
     </li> 
    </ol> 
    <input type="submit" value="Log in" /> 
</fieldset> 
    } 
    </section> 
+2

那麼,在你的窗體中是否存在像'這樣的字段? – 2014-09-13 10:23:13

+0

@asdf_enel_hak不。用戶已登錄。因此,我想要獲取存儲在UserProfile表中的用戶標識。 – sensahin 2014-09-13 10:25:31

回答

1

你應該試試下面這個。 我想你錯過了幾件你現在可以與自己的邏輯進行比較的東西。

using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

class Program 
{ 

    private static void Main(string[] args) 
    { 
     UsersContext context = new UsersContext(); 



     string currentUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
     if (string.IsNullOrWhiteSpace(currentUserName)) 
     { 
      currentUserName = HttpContext.Current.User.Identity.Name; 
     } 
     var currentUser = (from u in context.UserProfiles 
          where u.UserName.Equals(currentUserName) 
          select u).FirstOrDefault(); 

     if (currentUser != null) 
     { 
      context.Employee.Add(new Employee() { EmployeeName = "Mr. Codebased", UserID = currentUser.UserId }); 
      context.SaveChanges(); 

      var userProfile = context.Employee.Include("User").FirstOrDefault().User; 
     } 

    } 

    public class UsersContext : DbContext 
    { 
     public UsersContext() 
      : base() 
     { 
     } 

     public DbSet<UserProfile> UserProfiles { get; set; } 
     public DbSet<Employee> Employee { get; set; } 
    } 

    [Table("UserProfile")] 
    public class UserProfile 
    { 
     [Key] 
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
     public int UserId { get; set; } 
     public string UserName { get; set; } 
    } 

    [Table("Employee")] 
    public class Employee 
    { 
     [Key] 
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
     public int EmployeeID { get; set; } 
     public string EmployeeName { get; set; } 

     public int UserID { get; set; } 
     public UserProfile User { get; set; } 
    } 

} 
+0

其實,我已經有了userid,因爲只有註冊和登錄用戶才能提交表單。所以我已經在UserProfile中使用了userid。但是我不知道如何從那裏得到這個用戶標識,並在用戶提交表單時將其存儲到Employee表中。 – sensahin 2014-09-13 10:38:12

+1

您使用哪種身份驗證 - 表單? – codebased 2014-09-13 10:43:26

+1

查看我的上述更新 – codebased 2014-09-13 10:44:51

相關問題