0

我刚刚制作了一个包含 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..

4

2 回答 2

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。

但是,如果您的视图是强类型的,在您的情况下是对象“Klant”,您不需要用请求填充值,该对象将自动填充,如果您的对象有一个属性 Template 它会也被填充。

于 2012-04-18T10:49:31.710 回答
0

我发现有时有一个nameid输入一起解决这个问题。

您为模板控件呈现的 html 是什么样的?

于 2012-04-18T12:16:28.470 回答