I need to create a linq statement that combines 2 tables and send that to the view.
Do I need to create a ViewModel for this.
Say the output will be the following
Table1.Vendor Table1.VendorName Table2.Address
I need to create a linq statement that combines 2 tables and send that to the view.
Do I need to create a ViewModel for this.
Say the output will be the following
Table1.Vendor Table1.VendorName Table2.Address
It depends what your result type is.
If you return a dynamic object an anonymous type then you can't put that into the ViewBag directly (see: Stuffing an anonymous type in ViewBag causing model binder issues). But if you're returning something that's strongly typed, you can just put that straight into the ViewBag and bypass having a model.
That said, I'd always lean towards having a strongly typed model!
Yes You Should use View-model because if u want to send more than one table you may use ViewBag or something equivalent viewdata[] that's wrong etc.. Because ViewBag for example is dynamic type it can't be sent to view with 2 table For example if you do something like this
ViewBag.Workers=db.Workers.Join(idb.AspNetUsers, e => e.UserID, i => i.Id, (e, i) => new { UserName = i.UserName, SBIN = e.SBIN, }).ToList();
You will Get error 500 when you use this viewbag in your View
<span>@ViewBag.Workers.Anything</span>
Even in this example you should use for-each you still get error becausethe ViewBag is internal So You could use something like that in the controller for this issue
var Workers=db.Workers.Join(idb.AspNetUsers, e => e.UserID, i => i.Id, (e, i) => new WorkerViewCardModelcs{ UserName = i.UserName, SBIN = e.SBIN, }).ToList();
return PartialView("_WorkerCard", Workers);
and Don't for get to use @model in the View or Partial View
@model IEnumerable<FinalProject.ViewModels.WorkerViewCardModelcs>
@foreach (var item in Model)
{
What ever logic you wish
}