MVC 5帮助理解ViewModel

时间:2016-04-12 16:54:07

标签: c# asp.net-mvc

好的,所以我有了我的ViewModel并且我理解了控制器的作用,我只是在实现它时遇到了困难。我不知道如何为ViewModel编写控制器代码,我自己尝试过研究它并且找不到任何东西。

这是我的viewModel,我将如何构建控制器?不要求你为我做,只是如何做到这一点

public class ViewOrderViewModel
{
    //From ORDER Table
    public int OrderId { get; set; }
    public System.DateTime OrderDate { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Email { get; set; }
    public decimal Total { get; set; }


    //from Products
    public List<Product> Products { get; set; }
}

更新

public class ViewOrderController : Controller
    {
        // GET: ViewOrder
        public ActionResult ViewOrders()
        {

            var order = new Order();
            var viewModel = GetViewModel(order);

            return View(viewModel);
        }



        public ViewOrderViewModel GetViewModel(Order orderObject)
        {
            ViewOrderViewModel viewModel = new ViewOrderViewModel();

            viewModel.OrderId = orderObject.OrderId;
            viewModel.OrderDate = orderObject.OrderDate;
            viewModel.FirstName = orderObject.FirstName;
            viewModel.LastName = orderObject.LastName;
            viewModel.City = orderObject.City;
            viewModel.Address = orderObject.Address;
            viewModel.Country = orderObject.Country;
            viewModel.Email = orderObject.Email;
            viewModel.PostalCode = orderObject.PostalCode;
            viewModel.Total = orderObject.Total;



            return viewModel;
        }
    }

仍然不确定如何将ViewModel类中的产品列表映射到db中的产品列表

1 个答案:

答案 0 :(得分:2)

通常,您的视图将“绑定”到ViewModel。这就像说“好吧,我是'订单'的视图,我只需要担心你在ViewOrderViewModel中定义的属性。”

该绑定不需要控制器。绑定在视图的顶部声明:

<强> Order.cshtml

@Model MyProject.Web.ViewModels.ViewOrderViewModel
<div>
    <!-- Html for the view-->
</div>

这允许您在视图中访问该模型的属性。剃刀具有一些让生活变得轻松的功能。例如,如果要显示OrderId,它可能如下所示:

<span>OrderId: @Model.OrderId </span>

视图不关心为每个属性设置的值,它只关心属性是否存在。控制器进入游戏的位置是使用您想要的值填充这些属性,然后将ViewModel传递给视图:

    public ActionResult Order()
    {
        var viewModel = new ViewOrderViewModel();
        // Load data into each property
        viewModel.OrderId = 123; // etc..

        // Return it to the view. Asp.net knows to return
        // it to the Order.cshtml view because the view
        // and the controller action share the same name.
        return View(viewModel);
    }

编辑:在评论中回答您的问题:如果您需要使用来自不同模型(例如数据库模型)的值填充ViewModel,您可以创建一个映射器,如下所示: / p>

    public ViewOrderViewModel GetViewModel(Order orderObject)
    {
        ViewOrderViewModel viewModel = new ViewOrderViewModel();
        viewModel.OrderId = orderObject.OrderId;
        viewModel.FirstName = orderObject.FirstName;
        // etc...

        return viewModel;
    }

然后在你的控制器中你会做这样的事情:

// var order = new Order()
var viewModel = GetViewModel(order);