1

I currently have a model that is passing a dogs variable into the view:

public ActionResult Summary()
{
    var dogs = repository.GetAllRecords().OrderByDescending(x => x.DateRecieved);

    return View("Summary", dogs);
}

This has a field in it called OutcomeID, which references another array by ID:

    var categories = new List<Category>();
    try
    {
        this.Connect();
        var cmd = new SqlCommand
        {
            CommandText = "dbo.GetSummaryOutcomes",
            CommandType = CommandType.StoredProcedure,
            Connection = this.cn
        };
        var result = cmd.ExecuteReader();
        while (result.Read())
        {
            categories.Add(new Category
            {
                Name = result["Outcome"].ToString(),
                Value = result["id"].ToString(),
                InUse = bool.Parse(result["InUse"].ToString())
            });
        }
    }

Is there anyway so that on my view I can combine these, and instead of just having the OutcomeID, I reference the Outcome result from the categories list?

ie I need to get result["Outcome"] based on matching dogs.OutcomeID to result["id"]

Sorry if this sounds like a silly question, in PHP this wouldn't be a problem for me, but I'm very new to ASP.NET MVC and I'm not sure where to begin

Thank you

Nick
  • 3,745
  • 20
  • 56
  • 75

1 Answers1

4

You can create a View Model which is a common practice in ASP.NET MVC, basically you will have to create a new class with the properties that you need to pass on to your view, then in your controller you will send the instace of the view model to your view and also transform your data into the viewmodel.

Have a look here ASP.NET MVC - How exactly to use View Models. Also you might want to have a look at automapper, which is a mapping library that removes a lot of the boilerplate code involved when working with view models.

Community
  • 1
  • 1
ryudice
  • 36,476
  • 32
  • 115
  • 163