我使用mvc 5作为学习目的。 当我从下拉列表中发送控制器上的数据时,对象在控制器中具有空值。mvc5中的html帮手下拉控件的空值后
型号代码:
namespace Dropdownlist.Models
{
using System;
using System.Collections.Generic;
public partial class Country
{
public int ID { get; set; }
public string CountryName { get; set; }
}
}
控制器代码:
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);
}
}
}
查看代码:
@{
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>
}
我在做什么错?
你需要ID进行设置? – Hamed
为什么你认为它应该把数据发送给控制器 –