I have been browsing stack for an answer and I see so many different ways of doing this, none of them works when i try them though. so maybe I am doing something fundamentally wrong in my code.
I have a dropdownlistfor which looks like this:
@Html.DropDownListFor(m => m.PrivilegesGroups.First().Id, new SelectList(Model.PrivilegesGroups, "Id", "Name"), new { id = "PrivilegeGroupIdDDL", @class = "tableInput" })
This is my model.
namespace My.Internal.Models {
public class UserViewModel {
public User User { get; set; }
public IEnumerable<PrivilegeGroup> PrivilegesGroups { get; set; }
public IEnumerable<Company> UserCompanies { get; set; }
}
}
Where User is a class with an Id, Name, UsersCompanyId and PrivilegeGroupId.
PrivilegeGroup is a class with: Id,Name.
Company is a class with: Id,Name.
My controller for saving looks like this.
[HttpPost]
public IActionResult ManageUsers(UserViewModel model) {
UserDao db = new UserDao();
User modelToSave = new User();
db.AddUpdateUser(modelToSave);
return RedirectToAction("ManageUsers");
}
I have tried different ways of getting the value of the selected dropdownlist item but have yet to manage to have it come back in the UserViewModel.
Any help in how to get the ID of the selected item would be much appreciated.