Reputation: 18684
I am building a partial view which shows a list of accounts that a user can select. It's displayed on a Layout page, as they can switch between accounts at any time in their session.
In my controller, I have returned a list of accounts, and built a model for the partial view:
public ActionResult AccountPortfolios()
{
var personId = int.Parse(User.Identity.Name);
var ports = new AccountService().GetPortfoliosByPersonId(personId);
var result = new List<AccountPortfolioListModel>();
foreach (var port in ports)
{
result.Add(new AccountPortfolioListModel
{
Description = port.Description,
Id = port.Id
});
}
return View(result);
}
I am then passing the model (which is List<AccountPortfolioListModel>()
to my View.
In my view, I have the model declared as:
@model List<BasicFinanceUI.Models.AccountPortfolioListModel>
But then I am stuck. I want to display the list in a Drop Down, with a button to select the selected one, and call a Post method.
Additionally, am I using the right thing? A partial view to show a drop down list of something that is visible on my Layout page?
Upvotes: 0
Views: 103
Reputation: 6423
we like to use a static call for populating drop downs
@Html.DropDownListFor(x => x.Account, PathToController.AccountList())
then on your controller
public static List<SelectListItem> AccountList(){
List<SelectListItem> ls = new List<SelectListItem>();
//populate your list
return ls;
}
I don't know that you need a partial view for just a drop down. How I would do this is to put an input variable on the index action of the account
public ActionResult Index(string account)
then on your buttons click event
window.location = '@Url.Action("Index", "Controller", new { account = "----" })'.replace("----", $('#Account').val());
Upvotes: 2