我刚刚制作了一个包含DropDownList的表单,它完美地显示了选项名称,但没有将所选选项的ID发布到控制器中。
以下是控制器的代码:
[HttpGet]
public ActionResult Insert(int id)
{
TemplateRepository repo = new TemplateRepository();
List<Template> templateList = repo.ListAll().ToList<Template>();
ViewData["Template"] = new SelectList(templateList, "Id", "Omschrijving");
return View();
}
[HttpPost]
public ActionResult InsertOrEditSubmit(Klant klant)
{
KlantRepository repo = new KlantRepository();
klant.Naam = Request["Naam"];
klant.Adres = Request["Adres"];
klant.Postcode = Request["Postcode"];
klant.Woonplaats = Request["Woonplaats"];
klant.Email = Request["Email"];
klant.Telefoon = Request["Telefoon"];
repo.SaveOrUpdate(klant);
return RedirectToAction("Index");
}
以下是视图中的代码:
@using (Html.BeginForm("InsertOrEditSubmit", "Klant", FormMethod.Post))
{
@Html.DevExpress().Label(
settings =>
{
settings.ControlStyle.CssClass = "label";
settings.Text = "Template";
settings.AssociatedControlName = "Template";
}
).GetHtml() <br />
@Html.DropDownList("Template", ViewData["Template"] as SelectList);
@Html.DevExpress().Button(
settings =>
{
settings.ControlStyle.CssClass = "button";
settings.Name = "Insert";
settings.Text = "Toevoegen";
settings.UseSubmitBehavior = true;
}
).GetHtml()
@Html.DevExpress().Button(
settings =>
{
settings.ControlStyle.CssClass = "button";
settings.Name = "Cancel";
settings.Text = "Terug";
settings.ClientSideEvents.Click = "function(s, e){ document.location='" + DevExpressHelper.GetUrl(new { Controller = "Gebruiker", Action = "Index" }) + "'; }";
}
).GetHtml()
}
希望有人能解释为什么它不发布ID ..
答案 0 :(得分:0)
尝试:请求[“模板”];
尝试一个简单的测试:
(控制器)
ViewData["Test"] = new SelectList(new[] {new {Id = 1, Text = "Test 1"}, new {Id = 2, Text = "Test 2"}, new {Id = 3, Text = "Test 3"}}, "Id", "Text");
(查看)
@Html.DropDownList("Test", ViewData["Test"] as SelectList)
现在Request [“Test”]应该返回所选值的Id。
但是如果您的View是强类型的,在您的情况下对象“Klant”,您不需要使用Request填充值,对象将自动填充并且如果您的对象具有属性Template它也将被填充。
答案 1 :(得分:0)
我发现,有时输入name
和id
可以解决此问题。
模板控件的渲染html是什么样的?