我有一个模型,用于存储正在创建新记录的当前用户的身份。如何修改ActionResult Create()来捕获当前用户的名称并将其分配给paCrtdBy?
这是模型:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web;
namespace phaud.Models
{
public class phauds
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[Display(Name = "Created By")]
[MaxLength(128)]
public string paCrtdBy { get; set; }
private DateTime _createdOn = DateTime.Now;
[Required]
[Display(Name = "Record Date")]
[DataType(DataType.DateTime)]
public DateTime paCrtdDt {
get
{
return (_createdOn == DateTime.MinValue) ? DateTime.Now : _createdOn;
}
set
{
_createdOn = value;
}
}
}
}
这是CREATE()的控制器代码:
// GET: phauds/Create
public ActionResult Create()
{
ViewBag.DropDownValues1 = new SelectList(new[] { 3, 6, 9 });
ViewBag.DropDownValues2 = new SelectList(new[] { 9, 18, 27 });
return View();
}
// POST: phauds/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,paCrtdBy,paCrtdDt,repId,callId,callDt,q_opClsCall,q_asstOffer,q_reassrCaller,q_empathy,q_prof,q_recap,q_deadAir,cmnts")] phauds phauds)
{
string userName = User.Identity.Name;
//How do I assign the value of userName to the paCrtdBy field in the phauds.cs
//How to assign DateTime.Now to paCrtdDt instead of doing it in the model
if (ModelState.IsValid)
{
db.phauds.Add(phauds);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(phauds);
}