1

I'm new of asp.net mvc. I'm making a small application of shopping car. I use a shoppingCar view and use partialview to show products in shoppingcar view. And I want to check the BuyerName and Deliver data from ShoppingCarView and the products buyer select in ProductView.

How can I get the data from shoppingcarview and productsview. Is this practical or possible to do?How should I do.

ShoppingCarVM

public class ShoppingCarViewModel
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Buyer name could not be null")]
    [DisplayName("購買者姓名")]
    public string BuyerName { get; set; }

    //[System.Web.Mvc.Remote("DateValid", "StringValid", ErrorMessage = "日期必須在訂購日三天以後")]
    [DisplayName("送貨時間")]
    public DateTime DeliverDate { get; set; }

    public List<ProductViewModel> pList;
}

ProductVM

public class ProductViewModel
{
    [DisplayName("商品")]
    public string ProductName { get; set; }

    [DisplayName("價錢")]
    public int ProductPrice { get; set; }

    [DisplayName("數量")]
    public int ProductAmt { get; set; }

    [DisplayName("商品區")]
    public string ProductGroup { get; set; }

    [DisplayName("是否購買")]
    public bool ProductBuy { get; set; }
}

I use this line at main view giving pList to partialView

@Html.Partial("ShoppingCarProducts", Model.pList)

But when I want to post the data back at the controller

[HttpPost]
    public ActionResult ShoppingCar(ShoppingCarViewModel data)
    {
        if (ModelState.IsValid)
        {
            return View();
        }

        return View(data);
    }

the data.pList is always null.

Does anyone know how to fix it or another way to do this.

Thanks for read this.

moussesj94
  • 485
  • 1
  • 8
  • 27
  • The dupe explains who to bind to collection properties, but you models are incorrect anyway, and you would need something along the lines of `class ProductVM` with properties `string Name`, `int Amount` (not sure why you think you need a checkbox) and `ProductGroupVM` with properties `string Name` and `List Products` and the main view model will be `class ShoppingCartVM` with properties `string BuyerName`, `DateTime DeliveryDate` and `List Groups` (not sure what `BuyerName` is - surely that is the current user) –  Sep 14 '17 at 06:29
  • And as a side note, get rid of all those awful `id` attributes which are pointless anyway, and NEVER attempt to override the `name` attribute when using `HtmlHelper` methods –  Sep 14 '17 at 06:29

0 Answers0