How do I view the parent view model MVC3 C#?

2019-05-29 18:22发布

问题:

I want to use two models in one view and have looked at parent view model.

I have two models:

 public class Blogg
    {
        public int ID { get; set; }
        public string Blogg_name { get; set; }
        public string Message { get; set; }
        public string Blogg_subject { get; set; }

        private DateTime _Blogg_date = DateTime.Now;

        public DateTime Blogg_date
        {
            get
            {
                return _Blogg_date;
            }
            set
            {
                _Blogg_date = value;
            }
        }

    }

And

public class Dish
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string MainIngredient { get; set; }
        public string Cooking { get; set; }
        public string Drink { get; set; }
        public string Ingredient { get; set; }
        public string Type { get; set; }
    }

This is my parent view model:

public class DishBlogg
    {
        public Blogg Blogging { get; set; }
        public Dish Dishing { get; set; }
    }

How do I show the result in my view (Home/index.cshtml)? This is not working:

@model IEnumerable<XXXXXX.Models.DishBlogg>

@foreach (var item in Model)
      {
                @Html.DisplayFor(modelItem => item.Blogging.Blogg_date)
                @Html.DisplayFor(modelItem => item.Blogging.Blogg_subject)
                @Html.DisplayFor(modelItem => item.Blogging.Blogg_name)
                @Html.DisplayFor(modelItem => item.Blogging.Message)

    }

I can show one model with this (of course):

@model IEnumerable<XXXXXX.Models.Blogg>

   @foreach (var item in Model)
      {
                @Html.DisplayFor(modelItem => item.Blogg_date)
                @Html.DisplayFor(modelItem => item.Blogg_subject)
                @Html.DisplayFor(modelItem => item.Blogg_name)
                @Html.DisplayFor(modelItem => item.Message)

    }

But how do I show both (with DishBlogg)?

I get this error: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[XXXX.Models.Blogg]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[XXXX.Models.DishBlogg]'

Edit:

Here is the Index of HomeController where I pick out the last post from the database. I want to do the same with the dish and present both at the same view

 public ActionResult Index()
        {

            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }

回答1:

You need to adapt your view model:

public class DishBlogg
{
    public IEnumerable<Blogg> Blogging { get; set; }
    public IEnumerable<Dish> Dishing { get; set; }
}

and then try adding a .ToList() call to eagerly execute the queries in the controller:

public ActionResult Index()
{
    var model = new DishBlogg
    {
        Blogging = db.Bloggs.OrderByDescending(o => o.ID).Take(1).ToList(),
        Dishing = db.Dishes.OrderByDescending(o => o.ID).Take(1).ToList(),
    }
    return View(model);
}

and then in your view:

@model XXXXXX.Models.DishBlogg

@foreach (var blog in Model.Blogging)
{
    ...
}

@foreach (var blog in Model.Dishing)
{
    ...
}


回答2:

I think your problem is rather clear.

Your model is a DishBlog but you are passing a Blogg

Try this:

public ActionResult Index()
        {

            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1).FirstOrDefault();
            var list = new List<DishBlogg>();
            list.Add(new DishBlogg(){Blog = lastblog});

            return View(list);
        }


回答3:

if you want to pass the viewmodel "DishBlogg" to your view , you need to remove the IEnumerable from the model declaration in your view

In your view you are using @model IEnumerable<XXXXXX.Models.DishBlogg> which indicates that the model expected by the View is of type IEnumerable of DishBlogg.

However in the controller you are passing an object of type DishBlogg as a model return View(viewModel); hence the error.

Either change the view to have @model XXXXXX.Models.DishBlogg

or change your view model to return a list and pass a IEnumerable of DishBlogg as model to the view in controller

so instead of this @model IEnumerable<XXXXXX.Models.DishBlogg>
use this @model <XXXXXX.Models.DishBlogg>

change your viewmodel class to this

public class DishBlogg(Blogg blogg, Dish dish)
{
    this.LastBlogg = blogg;
    this.LastDish = dish;
}
public Blogg LastBlogg { get; private set; }
public Dish LastDish { get; private set; }


Fill and pass the view model to your view

public ActionResult Index()
{
    var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
    var lastdish = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
    var viewModel = new DishBlogg(lastblogg , lastdish );

    return View("Index", viewModel);
}