Updating multiple items within same view

2019-01-24 19:23发布

问题:

I am trying to make a stock take application, My view loads all my stock with one editor. My controller is not getting any of the data from the view?

I want to be able to edit all my stock at the same time? How can I do this

Model Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FlatSystem.Models
{
public class Stock
{
    public int ID { get; set; }
    public string Item_Name { get; set; }
    public int Actual_Stock { get; set; }
    public int Wanted_Stock { get; set; }


}
}

View Code

@model IEnumerable<FlatSystem.Models.Stock>

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="sidemenu">
<div class="sidemenu-heading">
    ReStock
</div>
<div class="div-body">
<table>
<tr>
    <th>
        Item Name
    </th>
    <th>
        Wanted Stock
    </th>
    <th>
        Stock On Hand
    </th>
    <th></th>
</tr>
@foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Item_Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Wanted_Stock)
    </td>
    <td>
    <div class="editor-field">
        @Html.EditorFor(modelItem => item.Actual_Stock)
        @Html.ValidationMessageFor(modelItem => item.Actual_Stock)
    </div>
    </td>
    @Html.HiddenFor(modelItem => item.ID)
    </tr>
    }
    </table>
    </div>
    </div>
<input type="submit" value="Submit" />
}

Controller Code

        [HttpPost]
    public ActionResult ReStock(List<Stock> stock)
    {
        foreach (var item in stock)
        {
            if (ModelState.IsValid)
            {
                GR.InsertOrUpdate(item);
            }
        }
        GR.Save();
        return RedirectToAction("Restock");
    }

回答1:

It's hard to answer your question without model class, but idea is that your edit inputs must contain index in name attribute.

Something like this:

@for(int i = 0: i < Model.Count(); i++)
{
    <tr>
    <td>
        @Html.DisplayFor(modelItem => Model[i].Item_Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model[i].Wanted_Stock)
    </td>
    <td>
    <div class="editor-field">
        @Html.EditorFor(modelItem => Model[i].Actual_Stock)
        @Html.ValidationMessageFor(modelItem => Model[i].Actual_Stock)
    </div>
    </td>
    @Html.HiddenFor(modelItem => Model[i].ID)
    </tr>
}

Added:

Sorry, thanks to Darin Dimitrov, you can't access IEnumerable by index, use List or Array.



回答2:

You could use editor templates. I would recommend you to first read the following article in order to understand why your code doesn't correctly bind the collection. Once you understand that you could do the following:

@model IEnumerable<FlatSystem.Models.Stock>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div class="sidemenu">
        <div class="sidemenu-heading">
            ReStock
        </div>
        <div class="div-body">
            <table>
                <thead>
                    <tr>
                        <th>Item Name</th>
                        <th>Wanted Stock</th>
                        <th>Stock On Hand</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @Html.EditorForModel()
                </tbody>
        </div>
    </div>
    <input type="submit" value="Submit" />
}

and now define a custom editor template for the Stock type which will automatically be rendered for each element of the collection (~/Views/Shared/EditorTemplates/Stock.cshtml) - the name and location of the editor template is important as it works by convention:

@model FlatSystem.Models.Stock
<tr>
    <td>
        @Html.DisplayFor(x => x.Item_Name)
    </td>
    <td>
        @Html.DisplayFor(x => x.Wanted_Stock)
    </td>
    <td>
    <div class="editor-field">
        @Html.EditorFor(x => x.Actual_Stock)
        @Html.ValidationMessageFor(x => x.Actual_Stock)
    </div>
    </td>
    @Html.HiddenFor(x => x.ID)
</tr>

Remark: You might also want to include the Wanted_Stock and Item_Name as hidden fields along with the ID in the editor template in order for their values to be sent to the server, because you don't have a corresponding input field for them.