public ActionResult AddFood()
{
if (Session["uid"] != null)
{
string email = Session["uid"].ToString();
var user = HomeController.Users.FirstOrDefault(x => x.Email == email);
var selectedFood = HomeController.Foods.FirstOrDefault(x => x.Name == Request.Form["selectedFood"]);
user.Foods.Add(selectedFood);
}
return RedirectToAction("Diet", "User");
}
< /code>
$(document).ready(function () {
$('#foodDropdown').select2({
placeholder: "Choose food",
allowClear: true
});
});
@Html.DropDownList("selectedFood", ViewBag.FoodList as SelectList, "Choose food", new { @class = "form-control", id = "foodDropdown" })
Add
< /code>
In html there's a DropDownList where you can select different food names and I want to use the selected name to get the whole food object but can't access to selected food name.
Request.Form["selectedFood"] returns null here, why?
I am guessing I should get the value in some different way when using @Html... but I don't know how
Подробнее здесь: https://stackoverflow.com/questions/795 ... null-value