谢谢阿门。
我有同样的问题,我的下拉列表可以从数据库中填充,但是 OrganisationID(在我的情况下)在创建新记录时没有进入数据库(在我的情况下,总是捕获 0) - 直到我刚刚改变ViewBag 的名称与下拉列表中的值相同(即两个 OrganisationID) - 正如您有帮助地指出的那样 - 现在它可以工作了!
对于它的价值,对于其他任何人在我们的命名不一致以启用绑定时经历“绝望的编码器”和我所经历的挫折,这就是我用来使下拉列表工作的方法(抱歉 - 不使用实体框架,但如果你使用 EF,原则应该仍然清晰且易于适应):
但关键是要启用绑定的相同命名。再次感谢阿门!
模型
public class Organisation_Names
{
public DataSet GetOrg_Names()
{
SqlConnection cn = new SqlConnection(@"Data Source=XXXXXXXXX;User ID=XXXXXXXXX;Password=XXXXXXXXXXX;Initial Catalog=XXXXXXXXXXXX");
SqlCommand cmd = new SqlCommand("sp_GetOrg_Names", cn);
cn.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}
控制器
//
// GET: /Services/Create
**public ActionResult Create(Organisation_Names organisation_names)
{
DataSet ds = organisation_names.GetOrg_Names();
ViewBag.OrganisationID = ds.Tables[0];
List<SelectListItem> items = new List<SelectListItem>();
foreach (System.Data.DataRow dr in ViewBag.OrganisationID.Rows)
{
items.Add(new SelectListItem { Text = @dr["OrganisationName"].ToString(), Value = @dr["OrganisationID"].ToString() });
}
ViewBag.OrganisationID = items;
return View();
}
//
// POST: /Services/Create
[HttpPost]
[ValidateAntiForgeryToken]
**public ActionResult Create(CreateServiceModel createservicemodel, Organisation_Names organisation_names, FormCollection selection)
{
DataSet ds = organisation_names.GetOrg_Names();
if (ds == null)
{
return HttpNotFound();
}
ViewBag.OrganisationID = ds.Tables[0];
List<SelectListItem> items = new List<SelectListItem>();
foreach (System.Data.DataRow dr in ViewBag.OrganisationID.Rows)
{
items.Add(new SelectListItem { Text = @dr["OrganisationName"].ToString(), Value = @dr["OrganisationID"] + 1.ToString() });
}
ViewBag.OrganisationID = items;**
if (this.IsCaptchaVerify("Answer was incorrect. Please try again."))
{
try
{
int _records = createservicemodel.CreateService(createservicemodel.OrganisationID, createservicemodel.ServiceName, createservicemodel.ServiceDescription, createservicemodel.ServiceComments, createservicemodel.ServiceIdentificationNumber, createservicemodel.CreatedBy, createservicemodel.NewServiceID);
if (_records > 0)
{
return RedirectToAction("Index", "Services");
}
}
catch
//else
{
ModelState.AddModelError("", "Cannot Create");
}
}
{
return View(createservicemodel);
}
}
看法
@model WS_TKC_MVC4.Models.CreateServiceModel
@using CaptchaMvc.HtmlHelpers
@using WS_TKC_MVC4.Models
@{ViewBag.Title = "Service added by " ;} @User.Identity.Name
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"> </script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>CreateServiceModel</legend>
<div class="editor-label">
<p>Select Organisation</p>
</div>
<div class="editor-field">
@Html.DropDownList("OrganisationID")
@Html.ValidationMessageFor(model => model.OrganisationID)
@Html.EditorFor(model => model.OrganisationID)
</div>
(更多字段)
<div class="editor-label">
@Html.LabelFor(model => model.MathCaptcha)
</div>
@Html.MathCaptcha("Refresh", "Type answer below", "Answer is a required field.")
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>