2

Suppose I'm rendering another page(view) to a page (view). Now the nested view has its separate model. How to provide the model to the nested view.

Here is the example.

my Index Controller:

public ActionResult Index()
        {
            ViewBag.CreateModel = new Todo();
            return View(db.Todos.ToList());
        }

my Index View:

IEnumerable<ToDoMVC.Models.Todo>
@RenderPage("~/Views/Todo/Create.cshtml",ViewBag.CreateModel)

my Create View:

@model ToDoMVC.Models.Todo
// does operations with this model

Now, if I run the program it gives me some model type mismatch error for create view.

So, how to solve this? How to provide another model to a nested view from a view?

tereško
  • 58,060
  • 25
  • 98
  • 150

4 Answers4

0

In your model you would have a seperate model as a variable.

For example

  class Todo {
      public CreateModel create {get;set}
   }

Then when you pass through to the second view you would do something like this:

  @RenderPage("~/Views/Todo/Create.cshtml",Model.create)

That will pass that second model to your create page

Edit

Re-reading your question.

The mismatch will be because you are passing the whole model through and you want to pass an individual list item in.

So you need too loop through the model like this:

  foreach(var item in Model)
  {
      @RenderPage("~/Views/Todo/Create.cshtml", item)
  }
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
  • My model is like: `public class Todo { public int ID { get; set; } public string Title { get; set; } } public class TodoDBContext : DbContext { public DbSet Todos { get; set; } }` – Prasenjit Paul Feb 21 '14 at 09:43
  • And if I run the app in /create route everything works fine. What i want is to use the 'create' view inside some other view – Prasenjit Paul Feb 21 '14 at 09:44
0

Simply create a view model class like this:

public class ToDoViewModel
{
    public IEnumerable<ToDo> Todos{ get; set; }
    public ToDo NewToDoItem { get; set; } 
}

Then change your contoller's action:

public ActionResult Index()
{
     var todoViewModel = new ToDoViewModel(); 
     todoViewModel.NewToDoItem = new Todo();
     todoViewModel.Todos= db.Todos.ToList(); 
     return View(todoViewModel);
}

And then adjust your Index view:

@model ToDoMVC.Models.ToDoViewModel
@RenderPage("~/Views/Todo/Create.cshtml",Model.NewToDoItem)
Paweł Bejger
  • 6,176
  • 21
  • 26
0

You can create a MasterModel and refer it to index view

public Class MasterModel
{
   Public IEnumerable<ToDo> TodoList { get; set; }
   Public ToDo CreateModel { get; set; }  
}

Controller

 public ActionResult Index()
 {
       MasterModel model = new MasterModel();
       model.CreateModel = new Todo();
       model.TodoList  = db.Todos.ToList();
       return View(model );
 }

Refer this to Index model

@model MasterModel   
@RenderPage("~/Views/Todo/Create.cshtml",Model.CreateModel)
ssilas777
  • 9,672
  • 4
  • 45
  • 68
0

A view inside a view or inother words, a nested view is not viable as far as my affair with MVC goes. You create a view and bind it to a model to show data relevant to that model on the browser. Now if you want data from other multiple models / business entities to be rendered on the view then you use ViewModels (Reference 1,Reference 2) and for that matter, the answers above remain valid. You can also customize / define sections on your layout to render specific information aswell. Partial views also make an option depending on the kind of info you want delivered to the view.

Community
  • 1
  • 1
Aritra B
  • 1,726
  • 6
  • 29
  • 45