【问题标题】:How to create an input box (textbox) for a property of a model如何为模型的属性创建输入框(文本框)
【发布时间】:2011-03-12 01:40:06
【问题描述】:

我有一个包含商品列表的简单购物车模型:

public class ShoppingCart
{
    public List<Item> Items { get; set; }
    public double Tax { get; set; }
    // ... some other properties
}

public class Item
{
    public string Name { get; set; }
    public int Quantity { get; set; }
    public double Price { get; set; }
    public double TotalCost { get { return Quantity * Price; } }
}

我想修改特定商品的数量,我做了以下视图:

<%using (Html.BeginForm("Recalculate", "ShoppingCart", Model))
  { %>
<table id="cartTable" border ="5px" cellpadding="5px" cellspacing="5px" width="640">
    <tr>
        <td><b>Item Name</b></td>
        <td><b>Item Qty</b></td>
        <td><b>Item Price</b></td>
        <td><b>Subtotal</b></td>
    </tr>
    <%
    if (Model != null && Model.Items != null)
    {
        foreach (ShoppingCart.Models.Item item in Model.Items)
        {
            %>
            <tr>
                <td><%: item.Name%></td>
                <td><%: Html.TextBoxFor(m => m.Items[Model.Items.IndexOf(item)], new { @Value = item.Quantity })%></td>
                <td><%: String.Format("{0:C}", item.Price)%></td>
                <td><%: String.Format("{0:C}", item.TotalCost)%></td>
            </tr>
            <%
        }
    } 
    %>
    <!-- some other rows/columns go here -->
</table> 
<input type="submit" value="Update Cart" />
<%} %>

还有我的控制器:

public class ShoppingCartController : Controller
{

    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ActionResult Show(ShoppingCart model)
    {
        if (model!= null && model.Items == null)
        {
            List<Item> items = new List<Item>();
            items.Add(new Item { Name = "Hat", Price = 20.0, Quantity = 1 });
            items.Add(new Item { Name = "Snowboard", Price = 430.0, Quantity = 1 });
            items.Add(new Item { Name = "Goggles", Price = 24.0, Quantity = 3 });

            model.Items = items;
            model.Tax = 6.5;
        }
        return View(model);
    }

    [HttpPost]
    public ActionResult Recalculate(ShoppingCart model)
    {
        if (model != null && model.Items!=null)
        {
            foreach (Item item in model.Items)
            {
                if (item.Quantity == 0)
                {
                    model.Items.Remove(item);
                }
                else if (item.Quantity < 0)
                {
                    ModelState.AddModelError("error", "The quantity for " + item.Name + " must not be smaller than 0.");
                }
            }
        }
        return RedirectToAction("Show", "ShoppingCart", model);
    }

}

不幸的是,当我单击更新购物车按钮时,它会调用我的重新计算函数,但现在项目列表中的所有项目都是空的。如何保留商品并更新给定商品的数量?

在 BeginForm 函数中,我尝试传入当前模型,而根本不传递模型......没有任何变化。谁能帮我解决这个问题?

【问题讨论】:

    标签: asp.net-mvc forms model shopping-cart


    【解决方案1】:

    在适当的位置进行这些更改,一切都会开始工作。请注意,虽然回发价格和总成本不会填充到模型中,因为相应的呈现项目是静态文本。这些可以在控制器中重新填充或在视图中添加隐藏字段,以便可以发布和重新填充。

    <%using (Html.BeginForm("Recalculate", "ShoppingCart", FormMethod.Post))
    
    <td><%: Html.TextBoxFor(m => m.Items[Model.Items.IndexOf(item)].Quantity)%></td>
    
    //return RedirectToAction("Show", "ShoppingCart", model);
    return View("Show", model);
    

    【讨论】:

    • 这很有帮助...其他属性是如何通过表单传递的?我不希望它们是可编辑的,例如无需编辑项目名称或成本。
    • 出局时绑定,所以显示正常。但由于它们不会导致回发任何元素,因此在重新计算中,这些属性不会填充到模型中。为此,要么重新填充模型中的那些(例如来自 DB),要么具有隐藏字段。
    【解决方案2】:
     <%= Html.TextBox("Quantity", Model.Items[Model.Items.IndexOf(item)].Quantity ) %>
    

    这是一篇不错的博文,值得一读:http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

    【讨论】:

      猜你喜欢
      • 2013-03-26
      • 1970-01-01
      • 2019-04-28
      • 2016-09-08
      相关资源
      最近更新 更多