1

Okay, so i have my ViewModel and I understand what the controller does, I'm just having difficulty implementing it. I don't know how to code a controller for the ViewModel, i've tried researching it myself and can't find anything.

Here is my viewModel, how would I go about constructing the controller? Not asking you to do it for me, just how to do it

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

UPDATE

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

Still unsure about how to map the List of products in the ViewModel class to the list of products in the db

Andre Queen
  • 223
  • 4
  • 16

1 Answers1

2

Typically, your view is going to be "bound" to the ViewModel. It's like saying "Ok, I'm the view for an 'Order' and I only need to worry about the properties that you defined in ViewOrderViewModel".

The controller is not required for that binding to happen. The binding is declared at the top of your view:

Order.cshtml

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

This allows you to access properties on that model within the view. Razor has some functions that make life easy. For example, if you want to display the OrderId it might look like this:

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

The view doesn't care what values are set for each of those properties, it only cares that the properties exist. Where the controller comes into play is populating those properties with the values you want and then passing the ViewModel to the view:

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

Edit: In response to your questions in the comments: If you need to populate your ViewModel with values from a different model (such as a database model) you can create a mapper like so:

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

        return viewModel;
    }

and then in your controller you would do something like this:

// var order = new Order()
var viewModel = GetViewModel(order);
Elliott
  • 2,035
  • 20
  • 23
  • That genuinely is so helpful, thank you! Just a question, I follow you but when im defining data; viewModel.OrderId = 123; etc, how would I tell it to take the data from the order class? For instance something like viewModel.OrderId = Order.OrderId, How would that work? – Andre Queen Apr 12 '16 at 17:43
  • You would need to do exactly that: viewModel.OrderId = Order.OrderId. A common practice is to create a "Mapping" function where you pass in an Order object and it returns an instantiated ViewModel object. – Elliott Apr 12 '16 at 17:45
  • How would I create the mapping function? Sorry for the simplistic questions im still learning and this is greatly appreciated – Andre Queen Apr 12 '16 at 17:48
  • I updated my answer with a response to your question – Elliott Apr 12 '16 at 17:53
  • Thank you so much, Im learning a ton! How would I populate the Products field? As it isn't just one product but could be a list of products – Andre Queen Apr 12 '16 at 18:01
  • That's a broad topic that you'll want to step through a tutorial for. Here's a start: http://www.c-sharpcorner.com/UploadFile/mahesh/create-a-list-of-objects-in-C-Sharp/ – Elliott Apr 12 '16 at 18:05
  • Sorry I realized how broad that was, I already have all of my products in the database, all I need is for this to display which one is related to the order via OrderId – Andre Queen Apr 12 '16 at 18:10
  • How would I list all of the products in the order through the view model? – Andre Queen Apr 12 '16 at 18:38