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!