本文介绍了如何在不重定向的情况下遍历表并更新MVC 5中的单个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello House,



我正在研究MVC 5项目,我的视图中有一个购物车表,在表格中有一个文本字段用户可以在任何产品上更改数量的数量。我在页面的按钮处有一个按钮,我希望用户点击并更新他们新输入的数量。以下是我的代码:



Hello House,

I am working on MVC 5 projects, I have a shopping cart table in my views, and in the table there is a text field for quantity where users can change quantity on any product. I have a button at the buttom of the page where I want users to click and update their newly entered quantity. below is my code:

<table class="table">
                        <thead>
                            <tr>
                                <th colspan="2">Product</th>
                                <th>Quantity</th>
                                <th>Unit price</th>
                                <th>Discount</th>
                                <th colspan="2">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                        @foreach (var Item in Model)
                          {
                            <tr>
                                <td>
                                   <img src="@Item.FrontViewThumbNail" alt="@Item.ProductName">
                                </td>
                                <td>
                                   @Item.ProductSku.ToUpper()- @Item.ProductName
                                </td>
                                <td>
                                    <input type="number" value="@Item.ProductQuantity" class="form-control" min="1" id="quantity" name="quantity">
                                    <input type="hidden" value="@Item.ProductID" name="hiddens" />
                                </td>
                                <td> ₦@Item.ProductPrice.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</td>
                                <td> ₦@ViewBag.Discount</td>
                                <td> ₦@Item.ProductTotalPrice.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</td>
                                <td>
                                    @*<a href="#"></a>*@
                                    <a href="@Url.Action("ViewCart", "Home", new { id = Item.ProductID })" class="btn btn-xs btn-bricky tooltips" data-placement="top" data-original-title="Delete"></a>
                                </td>
                            </tr>

                            }
                        <tfoot>
                            <tr>
                                <th colspan="5">Total</th>
                                <th colspan="2">₦@ViewBag.CartTotal.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</th>
                            </tr>
                        </tfoot>


                    </table>



我希望能够相应地更新购物车数量表单上的文本值。我能够处理删除和插入,只剩下批量更新的部分,我将感谢任何帮助。我做了一些研究,但无法解决这个问题。



我尝试了什么:



帖子方法:


I want to be able to update cart quantity accordingly with the text value on the form. I was able to handle deleting and insertion, the only part that is left the batch update, I will appreciate any assistance. I have done a few research, but was unable to handle this.

What I have tried:

The Post Method:

[HttpPost]
    public ActionResult ViewCart(IEnumerable<string> quantity, string command,IEnumerable<string> hiddens)
    {

        if (command == "UpdateCart")
        {
            var getRecordForUpdate = (from u in db.ShoppingCarts.Where(x => x.CartSessionID == Request.AnonymousID) select u).ToList();
            if(getRecordForUpdate !=null)
            {
                foreach (var quan in quantity)
                {
                    get
                }
            }
        }
}

推荐答案

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

namespace CartSample.Controllers
{
    public class CartController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            var model = new Order
            {
                Products = new List<Product>
                {
                    new Product
                    {
                        Name = "item 1",
                        Qty = 2,
                        Price = 9.99m
                    }
                }
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(Order model)
        {
            // Do something with udpated quantities
            foreach(var product in model.Products)
            {
                UpdateProductQty(product);
            }
            return View(model);
        }

        private void UpdateProductQty(Product product)
        {
            // Do something more useful than this example...
            var updatedQuantity = product.Qty;
        }
    }
    public class Order
    {
        public List<Product> Products { get; set; }
        public Order()
        {
            Products = new List<Product>();
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public int Qty { get; set; }
        public decimal Price { get; set; }
        public decimal Total()
        {
            return Price * Qty;
        }
    }
}





查看



注意:我如何使用Html.hiddenFor来确保内置MVC机制将现有购物车项目的表单字段映射到控制器中Index(post)操作的模型参数。

这样你就可以轻松查看提交给动作帖子的模型,并为每件物品更新数量做你想做的事。





View

Note: how I have used Html.hiddenFor to ensure the built in MVC mechanism maps the form fields for existing cart items to the model parameter for the Index (post) action in the controller.
That way you can easily look in the model submitted to action post and do what you want with updated Qty for every item.

@model CartSample.Controllers.Order
<html>
<head><title>Cart Sample</title>
</head>

<body>
    @using (Html.BeginForm("Index", "Cart", FormMethod.Post))
    {
        <table>
            <tr>
                <th>Name</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
            @for (int i = 0; i < Model.Products.Count; i++)

            {

                <tr>
                    <td>@Html.HiddenFor(x=>x.Products[i].Name)
                        @Html.DisplayFor(x=>x.Products[i].Name)</td>
                    <td>@Html.TextBoxFor(x=>x.Products[i].Qty)</td>
                    <td>@Html.HiddenFor(x => x.Products[i].Price)
                        @Html.DisplayFor(x => x.Products[i].Price)</td>
                    <td>@Model.Products[i].Total()</td>
                </tr>
            }
            <tr>
                <td>
                    <input type="submit" value="update" />
                </td>
            </tr>
        </table>
    }
</body>
</html>





这导致以下屏幕

[]


[HttpPost]
    public ActionResult ViewCart(int Id)
    {
       var getRecordForUpdate = (from u in db.ShoppingCarts.Where(x =>x.CartSessionID == Id) select u).FirstOrDefault();
            if(getRecordForUpdate !=null)
            {
                //do your stuff here
            }
        }
}





这将帮助您对单个选定的值或模型采取行动。



This will help you to make action for the single selected value or model whatever you are binding.


这篇关于如何在不重定向的情况下遍历表并更新MVC 5中的单个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:09