Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Updating multiple items within same view

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");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...