简体   繁体   中英

How to bind controls with models in MVC 4

I want to bind controls on my razor view but getting error:

here is my Order model:

    public class OrderViewModel
    {
        public int Id { get; set; }


        public AreaCatalog Area { get; set; }

        [Display(Name="Sub Total")]
        public int SubTotal { get; set; }

        public int Discount { get; set; }

        [Display(Name = "Delivery Fee")]
        public int DeliveryFee { get; set; }

        [Display(Name = "Total Amount")]
        public int TotalAmount { get; set; }

        public List<ProductViewModel> Product { get; set; }

    }

that is included in ProductOrderViewModel as below:

    public class ProductOrderViewModel
    {
        public List<ProductViewModel> Products { get; set; }

        public List<OrderViewModel> Orders { get; set; }
    }

here is my controller code:

        public ActionResult Edit()
        {
            ResponseModel result = new ResponseModel();
            result = new Logic().GetProducts();
            //var model = new ProductOrderViewModel() { Products = result.Model, Orders = yy };
            var model = new ProductOrderViewModel() { Products = result.Model };
            if (result.Success)
                return View(model);

            return View();

        }

and here is my view code:

@model ProductOrderViewModel
@using Helpers
@using ViewModels;
@{
    ViewBag.Title = "Edit";
}

@Html.EnumDropDownListFor(m => m.Orders.Area, new { @name = "area", @style = "width:295px; height:25px;margin-left:5px;" })

and Error is :

does not contain a definition for 'Area' and no extension method 'Area' accepting a first argument of type 'System.Collections.Generic.List

Please guide me through this problem

as error says: does not contain a definition for 'Area' and no extension method 'Area' accepting a first argument of type 'System.Collections.Generic.List

Area is property of your OrderViewModel , but not List<OrderViewModel> .

and you are actually trying to do something like:

//Assume:
List<OrderViewModel> myList = new List<OrderViewModel>();

and you are trying to draw EnumDropDownlistFor myList.Area . Which is not EVER possible. because Area is not a property of Generic List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM