I am building my first MVC site using MVC 4 in C# and Entity Framework 4.1. I have a model that displays a list of items with standard “add”, “edit”, “details”, and “delete” options.
The database that’s linked to the model has a column for “Username” and my application currently displays a text box for the user to enter their username via the following code from the Create.cshtml file.
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
I’m trying to alter this so that their User.Identity.Name (e.g. DOMAIN\user) value will either auto-populate in the text box, or even better, just be stored without showing the user that it’s happening.
Is there a relatively painless way to accomplish this?
EDIT: My Controller code for the “create” section currently looks like this:
public ActionResult Create(Occurrence occurrence)
{
if (ModelState.IsValid)
{
db.Occurrences.Add(occurrence);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "EmployeeName", occurrence.EmployeeID);
return View(occurrence);
}
You can set the username in the controller
another option is to handle it in the view