0

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);
        }
2
  • You can disable RegisteredBy if the model is to create. Commented Dec 18, 2019 at 14:26
  • Not disable, but filled from the Controller Create method Commented Dec 18, 2019 at 14:55

1 Answer 1

1

For registeredby and registeredDate fields;

You could remove them completely from the view and just assign there values on your Create POST method. Put them before ModelState.IsValid so that it would pass the validation for those properties.

vehicle.RegisteredDate = DateTime.Now(); // watch out, this is the server time
vehicle.RegisteredBy = "admin";

See the full post method below;

// controller code block        
 public ActionResult Create([Bind(Include = "Id,VehicleType,Amount,RenewPeriod,Status,ModifiedBy,ModifiedDate")] Vehicle vehicle)
{
   // assign here
   vehicle.RegisteredDate = DateTime.Now(); // watch out, this is the server time
   vehicle.RegisteredBy = "admin";

   if (ModelState.IsValid)
   {
      db.Vehicle.Add(vehicle);
      db.SaveChanges();
      return RedirectToAction("Index");
   }
   return View(vehicle);
}

For the status Field, on your Create method (NOT POST) you could instantiate the object, assign its values, and pass it to the view;

Be sure to include the Status field in your View

public ActionResult Create()
{
   Vehicle newVehicle = new Vehicle();
   newVehicle.Status = "New";
   return View(newVehicle);
}
Sign up to request clarification or add additional context in comments.

1 Comment

RegisteredBy and RegisteredDate can also be added the same way as Status here, if they want to be displayed on the initial Create form. I would disable those fields to prevent editing before submission. I also find it's better to get dates from the database server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.