Harshitha Kumar
Harshitha Kumar

Reputation: 41

MVC View Does Not Contain Expected Data

I have a view(EditView) that should display the data from 2 models. For this I put 2 models in one class like

public class MergeModel
{
    public Reservation Reservation { get; set; }
    public ViewTrain ViewTrain { get; set; }

}

and in the view I put the first line as @model MergeModel. But my problem is I'm sending only ViewTrain model's data to EditView and not Reservation model's data, so its displaying a error like the view is expecting MergeModel but you are sending ViewTrain model. So what should I do now to solve this? In detail: I have a ViewTrain model which contains trainId,TrainName,StartPlace and EndPlace and I have Reservation model which contains ReserveId,trainId,TrainName,StartPlace,EndPlace,NeedNoOfSeats. I have a view in which if I enter startPlace and EndPlace it will show the relevant trains available. On a particular train detail, if i click on select option a new view should be rendered in which all these details like trainId,TrainName,StartPlace and endPlace should be autofilled and NeedNoOfSeats textbox should be available to enter the number of seats, but Im not able do this..

Upvotes: 2

Views: 118

Answers (2)

Mehmet
Mehmet

Reputation: 755

1) You should add Constructor method to MergeModel Class

public MergeModel()
    {
        Reservation = new Reservation();
        ViewTrain = new ViewTrain();
    }  

2) In the action method you should return a MergModel object

MergeModel model=new MergeModel();  
//fill data to model  
return View(model);

Upvotes: 1

Manik Arora
Manik Arora

Reputation: 4792

Seems like the class Reservation might be null, create a instance of that class.

Like this -

new MergeModel{ new Reservation(), new ViewTrain() };

Hope this helps, if doesn't then post more details.

Upvotes: 0

Related Questions