asp.net mvc - How to bind Linq Multiple models data in a View using ViewBag MVC? -
i using linq
join multiple model classes , pass linq
object view
using viewbag
.
i 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") }; vehicledatacontext objdatacontext = new vehicledatacontext(); var vehcilesalesdetails = 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; return view(objsalesmodle); }
view: have problem while binding data, how call linq column names in 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>
viewbag has dynamic construction , don't recommend return object in viewbag. maintanence diffucult dynamics.
you can either add other properties salesmodel
or create new model return view vehiclesalesdetailmodel
. recommend create new model like:
public class salesmodel { public string name { get; set; } //change xxx make's type public xxx make {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 year{ get; set; } }
and in action:
public actionresult createvehiclesalesview() { var make = objvehiclecontext.vehiclemakes; salesmodel objsalesmodle = new salesmodel() { makeslist = new selectlist(make, "makeid", "make") }; vehicledatacontext objdatacontext = new vehicledatacontext(); vehiclesalesdetailmodel vehcilesalesdetails = 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 vehiclesalesdetailmodel { name = cus.customername, age = cus.age, city = cus.city, licenseid = cus.licesnseid, make = vmake.make, model = vmodel.model, year = vmodel.year, saledate = vs.saledate }; return view(vehcilesalesdetails); }
Comments
Post a Comment