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!!!
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);
}
.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
42 Points
184 Posts
select attributes of other model classes for creating drop down
Jul 31, 2020 09:38 AM|scala_1988|LINK
I have a model class,
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,
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!!!
Contributor
4923 Points
4198 Posts
Re: select attributes of other model classes for creating drop down
Jul 31, 2020 04:52 PM|DA924|LINK
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.
https://github.com/darnold924/PublishingCompany
Contributor
2690 Points
774 Posts
Re: select attributes of other model classes for creating drop down
Aug 03, 2020 07:26 AM|YihuiSun|LINK
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
Here is the result.
Best Regards,
YihuiSun
Contributor
4923 Points
4198 Posts
Re: select attributes of other model classes for creating drop down
Aug 03, 2020 01:20 PM|DA924|LINK
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/