i had field in user table forighn key refer to another table(department table) i want when i get user profile page to edit his information get this field drobdown list its data get from the table department
get this field drobdown list its data get from the table department
Do you ask about how to retrieve data from a table to a dropdown ?
( Are you working in MVC or .NET Core MVC ? I do not know yet what tutorial to send to you from
https://dotnet.microsoft.com/learn )
According to your needs, I wrote an example, you can refer to it.
You can use ViewBag.Department to store all the Departments, and then use its value on the view.
new SelectList(ViewBag.Department, "DepartmentID", "DepartmentName",
item.DepartmentID)
The fourth parameter: set the selected value.
Model
public class User
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public int DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public Department Department { get; set; }
}
public class Department
{
[Key]
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public List<User> Users { get; set; }
}
Controller
public class TestSelectController : Controller
{
public DailyMVCDemoContext db = new DailyMVCDemoContext();
public ActionResult Index()
{
var userwithdepartment=db.Users.Include(m => m.Department).ToList();
ViewBag.Department = db.Departments.ToList();
return View(userwithdepartment);
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
8 Points
22 Posts
validation of user profile
Feb 10, 2021 09:21 AM|samaremad|LINK
hello everyone
i had field in user table forighn key refer to another table(department table) i want when i get user profile page to edit his information get this field drobdown list its data get from the table department
how can i do that?
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: validation of user profile
Feb 10, 2021 02:19 PM|ignatandrei|LINK
Do you ask about how to retrieve data from a table to a dropdown ?
( Are you working in MVC or .NET Core MVC ? I do not know yet what tutorial to send to you from https://dotnet.microsoft.com/learn )
Member
8 Points
22 Posts
Re: validation of user profile
Feb 11, 2021 07:24 AM|samaremad|LINK
yes i want to retrive data from another table to a dropdown by using mvc
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: validation of user profile
Feb 15, 2021 03:33 PM|ignatandrei|LINK
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started
Contributor
2760 Points
787 Posts
Re: validation of user profile
Feb 26, 2021 08:37 AM|YihuiSun|LINK
Hi samaremad,
According to your needs, I wrote an example, you can refer to it.
Model
Controller
public class TestSelectController : Controller { public DailyMVCDemoContext db = new DailyMVCDemoContext(); public ActionResult Index() { var userwithdepartment=db.Users.Include(m => m.Department).ToList(); ViewBag.Department = db.Departments.ToList(); return View(userwithdepartment); } }
View
@model IEnumerable<DailyMVCDemo3.Models.User> <table class="table"> <tr> <td>UserId</td> <td>UserName</td> <td>DepartmentName</td> </tr> @foreach (var item in Model) { <tr> <td>@item.UserId</td> <td>@item.UserName</td> <td>@Html.DropDownListFor(m=>item.DepartmentID, new SelectList(ViewBag.Department, "DepartmentID", "DepartmentName", item.DepartmentID))</td> </tr> } </table>
Here is the result.
Best Regards,
YihuiSun