Search code examples
c#asp.net-mvccrudcontrollers

C# Model Binding modify property before 'db.SaveChanges'


I'm trying without success the following changes on a simple CRUD.

The APP contains 2 Models, 2 Controllers and the CRUD views for each Controller.

The 'Stores' and the 'Products' controllers. I need to create a new store and then create products related to that new store created

The 'Create' view has the following:

<div class="form-horizontal">
    <h4>Store</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
        </div>
    </div>

As you can see, 3 properties, Location, Description and Note. The controller code:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Location,Description,Notes,StoreCode")] Store store)
    {

        if (ModelState.IsValid)
        {

            db.Stores.Add(store);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(store);
    }

I'm trying to add in this bind model a fourth property called 'StoreCode' to insert in my database as

 StoreCode.DateTime.Now.ToFileTime(); 

But i don't want the user to create this on the view. I want to automatically assign this value on the database. Am i going on the right direction? Thanks


Solution

  • Don't pass it as a Model from the view. Just set it in the Controller.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Location,Description,Notes")] Store store)
        {
    
            if (ModelState.IsValid)
            {
                store.CreateDate = DateTime.Now;
                db.Stores.Add(store);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            return View(store);
        }