向成员列表添加值

时间:2019-07-08 03:17:17

标签: c# asp.net-mvc

我想用mvc创建一些购物网站,其中包含IdProduct Name PriceQuantity。因此,我只是为产品具有IdProduct Name Price的产品创建模型,当我在Web中输入内容时,将添加数量。在Controller中,我只是创建一个列表来设置模型的值。但是,当我添加“数量”的值时,该值刚刚列出了一个新列表。 如何将数量的值添加到列表的成员之一?

Barang listBarang = new Barang();
if (TempData.Peek("daftarBarang") == null)
{
    List<Barang> dftrBarang = new List<Barang>
    {
        new Barang{IdBarang = 1, NamaBarang = "Mouse", HargaBarang = 50000},
        new Barang{IdBarang = 2, NamaBarang = "Keyboard", HargaBarang = 100000},
        new Barang{IdBarang = 3, NamaBarang = "Monitor", HargaBarang = 2500000},
    };
    TempData.Add("daftarBarang", dftrBarang);
}
List<Barang> daftarBarang = (List<Barang>)TempData.Peek("daftarBarang");
return View(daftarBarang);```

输出是:

+-----------+-------------+-----------------+-----------+
| Id Barang | Nama Barang |    Harga Barang |  Quantity |
+-----------+-------------+-----------------+-----------+
|         1 | Mouse       |           50000 |         0 |
|         2 | Keyboard    |          100000 |         0 |
|         3 | Monitor     |         2500000 |         0 |
+-----------+-------------+-----------------+-----------+

当我提交数量时,这是一个新列表。那就错了

[编辑]查看

<form action="~/tambah" method="post">
            <table border="1">
                <tr>
                    <td> Id Barang</td>
                    <td> Nama Barang</td>
                    <td> Harga Barang</td>
                    <td> Quantity</td>
                </tr>
                @foreach (var item in Model)
                {
                    <tr>

                        {
                        <td>@item.IdBarang</td>
                        <td>@item.NamaBarang</td>
                        <td>@item.HargaBarang</td>
                        <td>
                            <input type="text" name="Quantity" />
                        </td>
                        <td>
                            <input type="submit" value="Submit"/>
                        </td>

                    </tr>
                }
            </table>
        </form>

1 个答案:

答案 0 :(得分:0)

抱歉,我的朋友迟到了。

这里是一个样本。希望对您有所帮助:))


//我添加了“数量”属性

public class BarangsViewModel
    {
        public int IdBarang { get; set; }
        public string NamaBarang { get; set; }
        public double HargaBarang { get; set; }
        public int Quantity { get; set; }
    }

//控制器

public ActionResult Products()
        {
            var products = new List<BarangsViewModel>();
            for (int i = 1; i < 5; i++)
            {
                var obj = new BarangsViewModel
                {
                    IdBarang = i,
                    NamaBarang = $"This is job {i}",
                    HargaBarang = i                    
                };
                products.Add(obj);
            }

            return View("Products", products);
        }



           [HttpPost]
            public IActionResult Products(List<BarangsViewModel> model)
            {
                return RedirectToAction("Index");
            }

//观看次数

@model List<WebApp.Models.BarangsViewModel>

@{
    ViewData["Title"] = "Products";
}
<h1>@ViewData["Title"]</h1>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <table id="mygrid" class="table" border="1">
        <thead>
            <tr>
                <td> Id Barang</td>
                <td> Nama Barang</td>
                <td> Harga Barang</td>
                <td> Quantity</td>
            </tr>
        </thead>
        <tbody id="rodetailrowdata">
            @{
                for (int i = 0; i < Model.Count; i++)
                {
                    <tr>
                        <td>
                            @Model[i].IdBarang
                            @Html.HiddenFor(model => Model[i].IdBarang)
                        </td>
                        <td>
                            @Model[i].NamaBarang
                            @Html.HiddenFor(model => Model[i].NamaBarang)
                        </td>
                        <td>
                            @Model[i].HargaBarang
                            @Html.HiddenFor(model => Model[i].HargaBarang)
                        </td>
                        <td>@Html.EditorFor(model => Model[i].Quantity, new {@type ="number" })</td>
                    </tr>
                }
            }
        </tbody>
    </table>
    <button type="submit">Submit</button>
}