I am doing my first project in ASP.NET MVC. I have generated views via scaffolding. So in my view (Create.cshtlml) there are fields for RegisteredDate, RegisteredBy, Status, ModifiedBy, ModifiedDate.
I want RegisteredDate (today's DateTime) and RegisteredBy ('admin') to be filled from controller Create method and Status filled already checked in the view page. Users should not fill these two fields RegisteredDate and RegisteredBy . How can I do that ?
code block
<div class="form-group">
@Html.LabelFor(model => model.RegisteredDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RegisteredDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RegisteredDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RegisteredBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RegisteredBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RegisteredBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Status)
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
</div>
// controller code block
public ActionResult Create([Bind(Include = "Id,VehicleType,Amount,RenewPeriod,RegisteredDate,RegisteredBy,Status,ModifiedBy,ModifiedDate")] Vehicle vehicle)
{
if (ModelState.IsValid)
{
db.Vehicle.Add(vehicle);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vehicle);
}
Createmethod