我有一個表單,用戶可以在其中創建新員工。我想將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>
那麼,在你的窗體中是否存在像'這樣的字段? – 2014-09-13 10:23:13
@asdf_enel_hak不。用戶已登錄。因此,我想要獲取存儲在UserProfile表中的用戶標識。 – sensahin 2014-09-13 10:25:31