模型与选择列表的绑定如何工作?

发布于 2024-10-11 01:19:19 字数 232 浏览 2 评论 0原文

我在检索表单集合中选择列表的值时遇到问题。我尝试使用与选择列表同名的属性创建视图模型。

老实说,我只是意识到我真的不明白模型绑定如何与选择列表一起使用。我只是假设以下约定适用:

  • 将选择列表命名为与您希望其绑定到的模型上的属性相同的名称。

除此之外,我真的不明白。我看过几本这方面的书,坦白说没什么用。

选择列表如何与 a) 表单集合和 b) 特定模型一起使用?

I'm having problems retrieving the values of a selectlist in my form collection. I've tried making a viewmodel with an attribute with the same name as the select list.

I'm honestly just realizing I REALLY don't understand how model binding works with selectlists. I've just been assuming that the following conventions apply:

  • Name the select list the same thing as the attribute on the model you want it to bind to.

Apart from that, I really don't get it. I've looked at several books on it and they're useless frankly.

How does a select list work with a) form collection and b) a particular model?

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

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

发布评论

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

评论(1

红墙和绿瓦 2024-10-18 01:19:19

这是一个示例:

模型:

public class MyViewModel
{
    public string SelectedItemValue { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fetch those from a repository
            Items = new SelectList(
                new[]
                {
                    new SelectListItem { Value = "1", Text = "Item 1" },
                    new SelectListItem { Value = "2", Text = "Item 2" },
                    new SelectListItem { Value = "3", Text = "Item 3" },
                }, 
                "Value", 
                "Text"
            )
        };
    }

    [HttpPost]
    public ActionResult Index(string selectedItemValue)
    {
        // Here you get the selected value from the dropdown
        return ...
    }
}

视图:

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

Here's an example:

Model:

public class MyViewModel
{
    public string SelectedItemValue { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Controller:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fetch those from a repository
            Items = new SelectList(
                new[]
                {
                    new SelectListItem { Value = "1", Text = "Item 1" },
                    new SelectListItem { Value = "2", Text = "Item 2" },
                    new SelectListItem { Value = "3", Text = "Item 3" },
                }, 
                "Value", 
                "Text"
            )
        };
    }

    [HttpPost]
    public ActionResult Index(string selectedItemValue)
    {
        // Here you get the selected value from the dropdown
        return ...
    }
}

View:

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