mercredi 15 février 2017

ASP.NET MVC 5 Update Database based on checkboxes

I am trying to update my database based on checkboxes I have in my View. What I want it to do is when I click "Submit" for the form to be passed to the HttpPost destroyMultipleBoxesUpload which is update the database boolean 'destoryed' to true if it is checked.

Here is my Controller

[Authorize]
public ActionResult destroyMultipleBoxes()
{
    var daybreakDB = new daybreakEntities3();
    List<box> list = daybreakDB.boxes.ToList();
    ViewBag.TodaysDate = DateTime.Today;
    return View(list);
}
[Authorize]
[HttpPost]
public ActionResult destroyMultipleBoxesUpload(HttpPostedFileBase file, FormCollection destroyBoxes)
{
    return RedirectToAction("Index");
}

Right now the destroyMultipleBoxesUpload does nothing.

my View:

 @model  List<DaybreakRecordsArchive.Models.box>
 @{
    ViewBag.Title = "destroyMultipleBoxes";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }

<h2>Destroy Multiple Boxes</h2>

<p>
    This webpage only loades boxes that are eligible for destruction.
</p>
<p>
    Please upload the destruction certificate then select the boxes that were destroyed under this certificate and click "Destroy".
</p>
@using (Html.BeginForm("destroyMultipleBoxesUpload", "Box", FormMethod.Post))
{
    <label for="file" id="imageLabel">Upload Image:</label>
    <input type="file" name="file" id="fileChoose" style="width: 100%;" />
    <table class="table">
        <th>
            Destroy
        </th>
        <th>
            BoxId
        </th>
        <th>
            Label Id
        </th>
        @foreach(var item in Model)
        {
            if (item.destruction_date < ViewBag.TodaysDate)
            {
            <tr>
                <td>
                    @Html.CheckBoxFor(modelItem => item.destroyed, false)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.id)
                    @Html.HiddenFor(modelItem => item.id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.labelId)
                    @Html.HiddenFor(modelItem => item.labelId)
                </td>
            </tr>
            }
        }
        <tr>
            <td>
                <input type="submit" name="Submit" value="Destroy" />
            </td>
        </tr>
    </table>
}

The Displays my database of boxes and shows the check boxes which is linked the a bool called destroyed and also 2 different identifiers.

 public partial class box
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public box()
        {
            this.residents = new HashSet<resident>();
            this.transitions = new HashSet<transition>();
        }

    public int id { get; set; }
    public Nullable<int> @class { get; set; }
    [DataType(DataType.Date)]
    public Nullable<System.DateTime> creation_date { get; set; }
    [DataType(DataType.Date)]
    public Nullable<System.DateTime> destruction_date { get; set; }
    public string owner { get; set; }
    public Nullable<int> facility { get; set; }
    public string description { get; set; }
    public bool destroyed { get; set; }
    public Nullable<int> current_location { get; set; }
    public string dest_img_path { get; set; }
    public string labelId { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<resident> residents { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<transition> transitions { get; set; }
    public virtual @class class1 { get; set; }
    public virtual storage_location1 storage_location { get; set; }
    public virtual facility facility1 { get; set; }
}




Aucun commentaire:

Enregistrer un commentaire