In ASP.NET MVC 5, I have a table that lists different dealerships to log into:
@foreach (var item in Model.Items) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name) - @Html.DisplayFor(modelItem => item.ID)
</td>
<td>
<input type="button" value="Login" onclick="location.href='@Url.Action("SelectDealership", new { id = item.ID, returnUrl = Request.QueryString["returnURL"]})'" />
</td>
</tr>
}
In the controller
[AllowAnonymous]
public ActionResult SelectDealership(int id)
{
//if a user with the ID exists in the database, then return all dealers that that user is associated with
DealerService dealerService = new DealerService();
var dealer = dealerService.GetDealersForDealership(id).FirstOrDefault(d => d.Email == Session["Email"].ToString() && d.Confirmed);
if (dealer != null)
{
if (!dealer.Dealership.IsActive)
{
ModelState.AddModelError(string.Empty, "Dealership has been deactivated!");
return View("Login");
}
Session["UserID"] = dealer.ID;
Session["DealershipID"] = id;
Session["DealerName"] = dealer.Dealership.Name;
Session["User"] = dealer.Username;
FormsAuthentication.SetAuthCookie(Session["Email"].ToString(), false);
UserPermissionService p = new UserPermissionService();
var user = p.getUsersPermissions(dealer.DealershipID, dealer.Email);
var admin = user.Where(u => (u.UserPermission == "IT" || u.UserPermission == "Customer Service" || u.UserPermission == "Marketing" || u.UserPermission == "Sales" || u.UserPermission == "Admin")).Where(m=>m.PermissionIsActive).ToList();
var IT = user.FirstOrDefault(u => u.UserPermission == "IT");
var marketing = user.FirstOrDefault(u => u.UserPermission == "Marketing");
ViewBag.isAdmin = admin.ToList().Any();
ViewBag.isIT = IT != null && IT.PermissionIsActive;
ViewBag.isMarketing = marketing != null && marketing.PermissionIsActive;
if (!string.IsNullOrEmpty(Request.QueryString["returnURL"]))
{
return Redirect(Request.QueryString["returnURL"]);
}
return RedirectToAction("Home", "Home");
}
ModelState.AddModelError("", "There was an error logging you in.");
return RedirectToAction("Login", "Home", null);
}
It returns a large table of the different logins each person has.
I want to replace it with a clean DropDownListFor element:
@using (Html.BeginForm("SelectDealership", "Admin", FormMethod.Post))
{
@Html.DropDownListFor(m => m.Selected,
Model.Items.Select(
x => new SelectListItem()
{
Text = $"{x.Name} - {x.ID}",
Value = x.ID.ToString(),
Selected = $"{x.Name} - {x.ID}" == Model.Selected
})
);
<input type="submit" />
}
I don’t know how to get this little bit on the table setup to go into the DropDownListFor:
, new { id = item.ID, returnUrl = Request.QueryString["returnURL"]}
How would that go in the DropDownListFor?