These are my tables
I want an output where the U_CCode
of first table, which is also a foreign key to my second table can be used to join both to show something like this (note : here I have passed A1
as parameter to where clause but I want it to dynamically select whatever value of U_CCode
I assign to the controller by a string variable. I want to achieve this using LINQ
and I am not able to understand how to use it in MVC controller.
I want to assign the output of this query to a selectlist
which I want to use in my view via viewbag
.
I am new to this, can someone help me with the script for this query?
CODE
public ActionResult Create(string U_CCode)
{
var departments = db.Class.Where(q => q.U_CCode == U_CCode);
ViewBag.SelectedDepartment = new SelectList(departments, "U_CLCode", "U_CLName");
}
Try like this: (EDITED)
var departmentsQuery = (from e in db.Class where e.U_CCode==U_CCode select e).ToList();
ViewBag.SelectedDepartment = departmentsQuery;
Pass it to the view..
In view:
@Html.DropDownList(
"name",
new SelectList(
((List<yourTypename>)ViewBag.SelectedDepartment).Select(x => new { Value = x.U_CCode, Text = x.U_CLName }),
"Value",
"Text"
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.