更新同一视图中的多个项目

更新同一视图中的多个项目

本文介绍了更新同一视图中的多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图做一个盘点应用,我的观点负载我所有的股票有一个编辑。
我的控制器没有从视图中获得任何数据?

我希望能够在同一时间来编辑我所有的股票?
我怎样才能做到这一点。

示范code

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间FlatSystem.Models
{
公共类股票
{
    公众诠释ID {搞定;组; }
    公共字符串ITEM_NAME {搞定;组; }
    公众诠释Actual_Stock {搞定;组; }
    公众诠释Wanted_Stock {搞定;组; }
}
}

查看code

  @model IEnumerable的< FlatSystem.Models.Stock>@using(Html.BeginForm())
{
@ Html.ValidationSummary(真)
< D​​IV CLASS =sidemenu>
< D​​IV CLASS =sidemenu航向>
    进货
< / DIV>
< D​​IV CLASS =格体>
<表>
&所述; TR>
    <第i个
        项目名
    < /第i
    <第i个
        诚征股票
    < /第i
    <第i个
        现有库存
    < /第i
    百分位>< /第i
< / TR>
@foreach(以型号VAR项)
{
&所述; TR>
    &所述; TD>
        @ Html.DisplayFor(modelItem => item.Item_Name)
    < / TD>
    &所述; TD>
        @ Html.DisplayFor(modelItem => item.Wanted_Stock)
    < / TD>
    &所述; TD>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(modelItem => item.Actual_Stock)
        @ Html.ValidationMessageFor(modelItem => item.Actual_Stock)
    < / DIV>
    < / TD>
    @ Html.HiddenFor(modelItem => item.ID)
    < / TR>
    }
    < /表>
    < / DIV>
    < / DIV>
<输入类型=提交值=提交/>
}

控制器code

  [HttpPost]
    公众的ActionResult进货(列表<股票及GT;股票)
    {
        的foreach(股票VAR项)
        {
            如果(ModelState.IsValid)
            {
                GR.InsertOrUpdate(项目);
            }
        }
        GR.Save();
        返回RedirectToAction(补货);
    }


解决方案

这是很难回答没有模型类的问题,但想法是,你的编辑输入必须包含指数在的name属性

事情是这样的:

  @for(INT I = 0:I< Model.Count();我++)
{
    &所述; TR>
    &所述; TD>
        @ Html.DisplayFor(modelItem =>产品型号[I] .Item_Name)
    < / TD>
    &所述; TD>
        @ Html.DisplayFor(modelItem =>产品型号[I] .Wanted_Stock)
    < / TD>
    &所述; TD>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(modelItem =>产品型号[I] .Actual_Stock)
        @ Html.ValidationMessageFor(modelItem =>产品型号[I] .Actual_Stock)
    < / DIV>
    < / TD>
    @ Html.HiddenFor(modelItem =>产品型号[I] .ID)
    < / TR>
}

补充:

对不起,由于达林季米特洛夫,则无法访问的IEnumerable 按索引,使用列表阵列

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");
    }
解决方案

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.

这篇关于更新同一视图中的多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:10