1

I have the following object, whose values are filled out in two separate forms.

public class MyObject
{
    public int ID { get; set; }

    //filled by student
    [StringLength(20)]
    public string Field1 { get; set; }

    //approved by teacher
    public bool Approve { get; set; }
}

Students create the initial record with Field1 filled in a form. Later, in the second form, the teacher reviews and approves the record by clicking the Approve checkbox and submits the form.

Here is the cshtml for the second form:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form">
        <div class="row">
            <div class="form-group">
                <label class="control-label">Field1</label>
                <br/>
                @Html.DisplayFor(model => model.Field1)
            </div>
        </div>

        <div class="row">
            <div class="form-group">
                <div class="checkbox">
                    <label>
                        @Html.EditorFor(model => model.Approve) Approve
                    </label>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group">
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </div>
}

Here is the controller for the second form

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ID,Approve")] MyObject myobject)
    {
        if (ModelState.IsValid)
        {
            db.Entry(myobject).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(myobject);
    }

However, when the teacher submits the form, the value of Field1 disappears in the database.

Note that the above object definition is just an example for the question. The actual object has many fields (booleans, strings, and even a binary field for images) that a student is required to fill.

Thanks!

curious1
  • 14,155
  • 37
  • 130
  • 231
  • 1
    Probably the easiest thing to do is to submit `Field1` as a hidden input. – Jasen Apr 11 '15 at 20:21
  • Jasen, thanks for your input! That could be very undesirable in terms of the quality (code, maintenance, etc.) of a solution. Also what about the image field? The image is saved in a database field. – curious1 Apr 11 '15 at 20:26
  • Bryan, thanks for chiming in! there are so many fields plus the binary field for the image. I believe there should be a cleaner solution. I came from the Java world, which allows the updates of only those fields which show up as form fields, leaving the rest intact. ASP.net should have something similar. – curious1 Apr 11 '15 at 20:38
  • The alternative is to keep track of the record id and what field the teacher updated. Then post the row id, query the db for the entity and update the changed field. – Jasen Apr 11 '15 at 20:38
  • Does ASP.NET MVC 5 support something called form backing object? This is how Java world retains the values of the object fields not updated in a form. The form backing object is loaded at form submission, combined with the fields in a form to form an updated object to save. – curious1 Apr 11 '15 at 20:57
  • [ViewModels](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc?lq=1) will break apart your `MyObject` into single-purpose classes. Together with partial views might provide you with a clean solution that you are looking for. – Jasen Apr 11 '15 at 21:02
  • Jasen, will look into ViewModels. Thanks for the info!! – curious1 Apr 11 '15 at 21:06

0 Answers0