Asked by:
select attributes of other model classes for creating drop down

Question
-
User364607740 posted
I have a model class,
namespace myapp.Models { public class AccountModels { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int AccountId { get; set; } [Required] public decimal AccountAmount { get; set; } [Required] [ForeignKey("ProjectId")] public ProjectModels project { get; set; } public int ProjectId { get; set; } [ForeignKey("SchoolId")] public SchoolModels School { get; set; } public int SchoolId { get; set; } } }
Inserting data for AccountModels, I need the drop down for SchoolName and ProjectName. These fields are present in the class ProjectModels and SchoolModels.
I did some research and watch some youtube videos but could not understand the process of how to do it.
I created a viewmodel for AccountModels,
namespace myapp.ViewModels { public class AccountViewModels { public IEnumerable<SchoolModels> schools { get; set; } public IEnumerable<karyakramModels> karyakrams { get; set; } public AccountViewModels accounts { get; set; } } }
Is this the right way for creating drop down? If this is not the correct approach, how should I do this?
Or, if this is the correct approach how to proceed further now?
Please help, I am new with MVC platform. Thank You!!!
Friday, July 31, 2020 9:38 AM
All replies
-
User1120430333 posted
You'll may want to look into SelectListItem.
https://nimblegecko.com/using-simple-drop-down-lists-in-ASP-NET-MVC/
Payrollcontroller, PayrollVM, PayrollDM and Payroll view Create work with a dropdownlist for Authors selection in the example project.
Friday, July 31, 2020 4:52 PM -
User1686398519 posted
Hi scala_1988,
You can use "DropDownListFor" and "ViewBag" to achieve your needs.I made an example, please refer to it.
Model(Only the modified and newly added models are given.)
public class AccountViewModels { public List<AccountModels> accounts { get; set; } } public class SchoolModels { [Key] public int SchoolId { get; set; } public string SchoolName { get; set; } } public class ProjectModels { [Key] public int ProjectId { get; set; } public string ProjectName { get; set; } }
Controller
public MVCTestContext db = new MVCTestContext(); public ActionResult Index() { AccountViewModels test = new AccountViewModels(); ViewBag.schoollist = new SelectList(db.SchoolModels.ToList(), "SchoolId", "SchoolName"); ViewBag.projectlist = new SelectList(db.ProjectModels.ToList(), "ProjectId", "ProjectName"); test.accounts = db.AccountModels.ToList(); return View(test); }
View
@model WebApplication26.Models.AccountViewModels <table class="table"> <tr> <td>AccountId</td> <td>AccountAmount</td> <td>School</td> <td>project</td> </tr> @foreach (var item in Model.accounts) { <tr> <td>@item.AccountId</td> <td>@item.AccountAmount</td> <td>@Html.DropDownListFor(m => item.School, new SelectList(@ViewBag.schoollist, "Value", "Text", item.SchoolId))</td> <td>@Html.DropDownListFor(m => item.project, new SelectList(@ViewBag.projectlist, "Value", "Text", item.ProjectId))</td> </tr> } </table>
Here is the result.
Best Regards,
YihuiSun
Monday, August 3, 2020 7:26 AM -
User1120430333 posted
You can use "DropDownListFor" and "ViewBag" to achieve your needs.I made an example, please refer to it.
It's not called Viewbag View Controller. It is called Model View Controller.
https://tech.trailmax.info/2013/12/asp-net-mvc-viewbag-is-bad/
Monday, August 3, 2020 1:20 PM