Ich benutze die mvc 5 für Lernzwecke. Wenn ich die Daten über den Controller aus der Dropdown-Liste sende, hat das Objekt einen Nullwert im Controller.null wert post auf controller von html helfer dropdown in mvc5
Modell-Code:
namespace Dropdownlist.Models
{
using System;
using System.Collections.Generic;
public partial class Country
{
public int ID { get; set; }
public string CountryName { get; set; }
}
}
Controller-Code:
namespace Dropdownlist.Controllers
{
public class HOMEController : Controller
{
DropDownEntities db = new DropDownEntities();
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Country cn)
{
db.Countries.Add(cn);
db.SaveChanges();
return View(cn);
}
}
}
Code anzeigen:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm(FormMethod.Post))
{
<div>
@Html.DropDownList("ddlcountry", new List<SelectListItem>
{
new SelectListItem{ Text = "India", Value = "India"},
new SelectListItem{ Text = "UK", Value = "UK"},
new SelectListItem{ Text = "USA", Value = "USA"}
}, "Select a Country")
</div>
<div>
<input type="submit" value="Save" />
</div>
}
Was mache ich falsch?
Haben Sie ID müssen gesetzt werden? – Hamed
Warum sollten Sie die Daten an den Controller senden? –