Pass multiple data objects to Controller to View? [Resolved]

Posted by Syedshakeer under ASP.NET MVC on 4/7/2017 | Points: 10 | Views : 284 | Status : [Member] | Replies : 1
I am using LINQ to join multiple model classes and pass the Linq object to View using ViewBag.

I am facing problem while repeating data or binding data

Model Properties:

public class SalesModel
{
public string CustomerName { get; set; }
public int SSN { get; set; }
public int LicenseId { get; set; }
public int Age { get; set; }
public string City { get; set; }
//Vehicle Sales
public DateTime? SaleDate { get; set; }
public int CustomerId { get; set; }
public int SelecteModle { get; set; }
}

public ActionResult CreateVehicleSalesView()
{
var make = objVehicleContext.VehicleMakes;

SalesModel objSalesModle = new SalesModel()
{
MakesList = new SelectList(make, "MakeId", "Make")
};

//objSalesModle object is using to bind Make dropdowlist in a view

VehicleDataContext objDataContext = new VehicleDataContext();
var vehcileSalesDetails = from VS in objDataContext.Vehiclesales
join VModel in objDataContext.VehicleModels on VS.ModelId equals VModel.ModelId
join Cus in objDataContext.Customers on VS.customerId equals Cus.customerId
join VMake in objDataContext.VehicleMakes on VModel.MakeId equals VMake.MakeId
select new
{
Name = Cus.customerName,
age = Cus.Age,
city = Cus.City,
licenseId = Cus.LicesnseId,
make = VMake.Make,
model = VModel.Model,
year = VModel.Year,
saleDate = VS.SaleDate


};
ViewBag.vehicleSalesDetails = vehcileSalesDetails; //How to pass this data to View?

return View(objSalesModle);
}


View: I have problem while binding data, How to call Linq column names in a view?

<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
<td>Make</td>
<td>Model</td>
<td>Year</td>
<td>Sale Date</td>
</tr>
@foreach (var v in ()ViewBag.vehicleSalesDetails)
{
<tr>@v.Name</tr>
<tr>@v.Age</tr>
<tr>@v.City</tr>
<tr>@v.Model</tr>
<tr>@v.Year</tr>
<tr>@v.SaleDate</tr>
}
</table>


Syed Shakeer Hussain


Responses

Posted by: Jitendrasoft09 on: 5/14/2017 [Member] [MVP] Starter | Points: 50

Up
0
Down

Resolved
Whatever vehcileSalesDetails is returning just convert into list of class and return back to view.
Otherwise, try it

var vehcileSalesDetails = from VS in objDataContext.Vehiclesales
join VModel in objDataContext.VehicleModels on VS.ModelId equals VModel.ModelId
join Cus in objDataContext.Customers on VS.customerId equals Cus.customerId
join VMake in objDataContext.VehicleMakes on VModel.MakeId equals VMake.MakeId
select new
{
Name = Cus.customerName,
age = Cus.Age,
city = Cus.City,
licenseId = Cus.LicesnseId,
make = VMake.Make,
model = VModel.Model,
year = VModel.Year,
saleDate = VS.SaleDate


}.ToList();

get similar details shared-
http://stackoverflow.com/questions/10994790/how-to-pass-list-from-controller-to-view-in-mvc-3

Jitendra Kumar
If my post helps you, plz mark as an answer.

Syedshakeer, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response