Error al usar dos modelos en la página de índice
Frecuentes
Visto 31 equipos
0
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
1 Respuestas
0
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
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5 or haz tu propia pregunta.
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