ASP.NET MVC App Request.Form gibt einen Nullwert an
Posted: 12 Apr 2025, 07:15
Code: Select all
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
Code: Select all
Request.Form["selectedFood"]
Ich vermute>