从数据库条目创建 DropDownList,然后在表单提交中绑定它

发布于 2024-09-08 07:08:58 字数 663 浏览 2 评论 0原文

我在数据库表中存储了枚举条目,其中仅包含以下字段:IDName。我想在表单上的 DropDownList 内显示存储在该表中的值。然后,用户选择一个值并提交表单。

找到了一种从枚举轻松创建 DropDownList 的方法(尽管最好只使用表中所有记录的 Name 字段填充 DropDownList)。但是,我还没有找到一种方法可以稍后将表单提交中的 DropDownList 绑定到一个整数值,以便与其他表单值一起放入数据库(FK - PK)。

您能否提供一些示例代码来说明如何进行此类绑定?

更新:感谢您的精彩回答。我还有一个问题:是否可以通过 AJAX 获取 DropDownList 内容,并将其放入 ViewModel 中的 DropDownList 和 SelectList 中(同时包含 ID 和 Name 参数)?我想根据用户的输入有选择地获取内容,并且我希望 ViewModel 然后填充所获取的数据。

I have entries for an enumeration stored inside a database table with only the following fields: ID and Name. I want to show the values stored inside this table inside a DropDownList on a form. The user then chooses a value and submits the form.

I found a way to easily create a DropDownList from an enumeration (although it would probably be best to just populate the DropDownList with the Name fields of all the records in the table). However, I haven't found a way to later bind the DropDownList in the form submission to an integer value to put into the database (FK - PK) with the other form values.

Can you provide some sample code that illustrates how to do such binding?

UPDATE: Thanks for the awesome answer. I have one more question: is it possible to fetch DropDownList content via AJAX and have it be put into the DropDownList and into the SelectList in the ViewModel (with both the ID and Name parameters)? I want to selectively fetch content based on an input the user makes and I want the ViewModel to then be filled with that fetched data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2024-09-15 07:08:58

一如既往,首先定义模型:

public class MyViewModel
{
    public int SelectedValue { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fetch from the database some enumerable collection 
            // containing Id and Name
            Items = new SelectList(new[]
            {
                new { Id = 1, Name = "item 1" },
                new { Id = 2, Name = "item 2" },
            }, "Id", "Name")
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // TODO: Do something with model.SelectedValue
        return RedirectToAction("index");
    }
}

最后是强类型视图:

<% using (Html.BeginForm()) { %>
    <%: Html.DropDownListFor(x => x.SelectedValue, Model.Items) %>
    <input type="submit" value="OK" />
<% } %>

As always start by defining a model:

public class MyViewModel
{
    public int SelectedValue { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Then the controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fetch from the database some enumerable collection 
            // containing Id and Name
            Items = new SelectList(new[]
            {
                new { Id = 1, Name = "item 1" },
                new { Id = 2, Name = "item 2" },
            }, "Id", "Name")
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // TODO: Do something with model.SelectedValue
        return RedirectToAction("index");
    }
}

and finally the strongly typed view:

<% using (Html.BeginForm()) { %>
    <%: Html.DropDownListFor(x => x.SelectedValue, Model.Items) %>
    <input type="submit" value="OK" />
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文