Error al usar dos modelos en la página de índice

I generated a simple MVC5 application (code first with scaffold for the views) and it generates for me the views for every action index, create, edit etc

I want to copy the code from the create view to the index view but in the index the model is declared like below :

@model IEnumerable<Ro01.Models.Ro>

and when I copy it to the view and run it I get an error. I can use only one model, how should I overcome this?

since the code of the create (razor code) having errors...

@model Role.Models.Ro

preguntado el 28 de mayo de 14 a las 12:05

1 Respuestas

You need to create a ViewModel. Create a ViewModels folder in your project and add a class like this:

public class FooViewModel
{

public IEnumerable<Ro01.Models.Ro> Model1 {get;set;}

public FooClass Model2 {get;set;}

}

Acción del controlador:

public ActionResult SomeAction()
{
FooViewModel model = new FooModel();
model.Model1 = new Ro01.Models.Ro();
mode.Model2 = new FooClass();

return View(model);
}

and then use it your View like this:

   @model Ro01.ViewModels.FooViewModel


@Html.HiddenFor(m=>m.Model1.SomeProperty)
@Html.HiddenFor(m=>m.Model2.SomeProperty)

contestado el 28 de mayo de 14 a las 13:05

Can you please guide me how to create the view model ?this is just regular model?And how should I refer to the fields from the model 1 and model 2? - Jean Tehhe

create a folder in project name ViewModels and in that add new class - Ehsan Sajjad

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.