我有一个请求表单,我想在会话IdEmpLogin
中获取用户的ID,以防我需要知道谁发出了请求
使用此模型
public class NewRequestViewModel
{
public int IdEmpLogin { get; set; }
public int IdEmpTarget{ get; set; }
public IDictionary<int, string> TargetUser{ get; set; }
public int EmpTargetCarId{ get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime RequestDate{ get; set; }
public int Status{ get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy HH:mm:ss}", ApplyFormatInEditMode = true)]
public <System.DateTime> ResponseDate { get; set; }
}
这是我的登录信息,对于我正在使用domain model
public ActionResult Login(String User, String Pass)
{
try
{
using (var db = new sir_cjfEntities())
{
var usr =(from d in db.employees
where d.user==User && d.password== Pass && d.status== true
select d).FirstOrDefault();
if (usr == null)
{
return Content("The username or password is incorrect");
}
else
{
Session["user"] = usr;
return Content("1");
}
}
}
catch (Exception ex)
{
return Content("Error" + ex.Message);
}
}
Domain Model
public partial class employees
{
public employees()
{
this.cars= new HashSet<cars>();
this.parking= new HashSet<parking>();
this.request= new HashSet<request>();
this.request2= new HashSet<request>();
}
public int id_employee{ get; set; }
public string name{ get; set; }
public string lname{ get; set; }
public string user{ get; set; }
public string password{ get; set; }
public Nullable<bool> status{ get; set; }
public virtual ICollection<cars> cars{ get; set; }
public virtual ICollection<parking> parking{ get; set; }
public virtual ICollection<request> request{ get; set; }
public virtual ICollection<request> request{ get; set; }
}
如何获取已登录员工的ID并将其保存在我的请求表中?
public ActionResult NewReq()
{
var model = new NewRequestViewModel
{
TargetUser= new Dictionary<int, string>()
};
using (var db = new sir_cjfEntities())
{
model.TargetUser= db.employees.ToDictionary(x => x.id_employee, x => x.name+ " " + x.lname);
}
return View(model);
}
[HttpPost]
public ActionResult NewReq(NewRequestViewModel model)
{
var db = new parkingDBEntities();
if (!ModelState.IsValid)
{
model.TargetUser= db.employees.ToDictionary(x => x.id_employee, x => x.name+ " " + x.lname);
return View(model);
}
var req= new request();
using (db)
{
model.RequestDate= DateTime.Now;
model.TargetUser= db.employees.ToDictionary(x => x.id_employee, x => x.name+ " " + x.lname);
req.status= null;
req.id_emp_logged = model.IdEmpLogin;//get logged in user id
req.id_emp_target = model.IdEmpTarget;//target user
req.emp_tar_car_id= model.EmpTargetCarId; //I have to get the car registered by the target user
req.request_date= model.RequestDate;
db.request.Add(req);
db.SaveChanges();
}
return Redirect(Url.Content("~/CRequest/"));
}
如您所见,我目前正在手动输入ID
这是视图
@model ParkingRequest.Models.ViewModels.NewRequestViewModel
@{
ViewBag.Title = "New Request";
}
<h2>@ViewBag.Title</h2>
<div class="row">
<div class="col-md-12">
@using (Html.BeginForm("NewReq", "CRequest", FormMethod.Post, new { }))
{
@Html.AntiForgeryToken()
@Html.ValidationMessage("Error", new { @class = "text-danger", @type = "text" })
@Html.LabelFor(d => d.IdEmpLogin)
@Html.TextBoxFor(d => d.IdEmpLogin, new { @class = "form-control" })
@Html.ValidationMessage("IdEmpLogin", new { @class = "text-danger" })
<br />
@Html.LabelFor(d => d.IdEmpTarget)
@Html.DropDownListFor(d => d.IdEmpTarget, new SelectList(Model.TargetUser, "Key", "Value"),
new { @class = "form-control" })
@Html.ValidationMessage("IdEmpTarget", new { @class = "text-danger" })
<br />
@Html.LabelFor(d => d.EmpTargetCarId)
@Html.TextBoxFor(d => d.EmpTargetCarId, new { @class = "form-control" })
@Html.ValidationMessage("EmpTargetCarId", new { @class = "text-danger" })
<br />
<div style="text-align:right;">
<input type="submit" class="btn btn-success" value="Apply for" />
</div>
}
</div>
</div>