I have one of a method as following in my controller
Code
var companies = ...
List<CompanyModel> listOfCompanies = new List<CompanyModel>();
foreach (var item in companies)
{
listOfCompanies.Add(new CompanyModel(item.CompanyId, item.CompanyName));
}
ViewBag.Company = listOfCompanies;
View page
@Html.DropDownListFor(m => m.Company, new SelectList(ViewBag.Company, "id", "name"), "Select company", new { @class = "form-control" })
this is working fine , I'm trying trying to use session instead of viewbag here.
So for that I did
var companies = ...
List<CompanyModel> listOfCompanies = new List<CompanyModel>();
foreach (var item in companies)
{
listOfCompanies.Add(new CompanyModel(item.CompanyId, item.CompanyName));
}
Session["CompanyData"] = listOfCompanies;
View page
@Html.DropDownListFor(m => m.Company, new SelectList(Model.Company, "id", "name", Session["CompanyData"]), "Select company", new { @class = "form-control" })
but this is not working properly, whats the path to achieve this
您是否尝试过简单地将Session
替换为ViewBag
?
@Html.DropDownListFor(m => m.Company, new SelectList((List<CompanyModel>)Session["CompanyData"], "id", "name"), "Select company", new { @class = "form-control" })
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.