English 中文(简体)
如何为模型的财产建立一个投入箱(文本框)
原标题:How to create an input box (textbox) for a property of a model

我有一个简单的购物车模式,其中载有下列物品清单:

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);
    }

}

不幸的是,当我点击《最新消息》Cartutton时,它叫我重新计算功能,但现在项目清单上的所有项目都是无效的。 我怎么能够保留这些项目并更新某个项目的数量?

在BeginForm职能中,我尝试采用目前的模式,而不是通过模式......没有任何变化。 谁能帮助我看上去?

最佳回答

适当地点和事物的这些变化是否开始运转。 请注意,虽然价格和总价格的回落不会在模型中填满,因为相应的物品是固定案文。 主计长可以重新接纳这些人员,或增加隐蔽领域,以便将这些人员张贴和重新接纳。

<%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);
问题回答
 <%= Html.TextBox("Quantity", Model.Items[Model.Items.IndexOf(item)].Quantity ) %>

这是一个 n格员额,值得一读:





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签