ASP.NET MVC - 与下拉列表的模型绑定

发布于 2024-10-02 13:32:45 字数 81 浏览 8 评论 0原文

是否可以拥有一个带有用于下拉列表的列表的视图模型,并且在发布表单时还可以从视图模型中获取下拉列表的选定值?

如果是这样,我该怎么做?

Is it possible to have a single view model with a list that is used for a dropdownlist and also get the selected value of the dropdownlist from the view model when I post a form?

If so, how can I do this?

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

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

发布评论

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

评论(1

小镇女孩 2024-10-09 13:32:45

当然,一如既往,首先定义视图模型:

public class MyViewModel
{
    public int? SelectedItemValue { get; set; }
    public IEnumerable<Item> Items { get; set; }
}

public class Item
{
    public int? Value { get; set; }
    public string Text { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fill the view model with data from
            // a repository
            Items = Enumerable
                .Range(1, 5)
                .Select(i => new Item 
                { 
                    Value = i, 
                    Text = "item " + i 
                })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // TODO: based on the value of model.SelectedItemValue 
        // you could perform some action here
        return RedirectToAction("Index");
    }
}

最后是强类型视图:

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

Sure, as always start by defining your view model:

public class MyViewModel
{
    public int? SelectedItemValue { get; set; }
    public IEnumerable<Item> Items { get; set; }
}

public class Item
{
    public int? Value { get; set; }
    public string Text { get; set; }
}

then the controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: Fill the view model with data from
            // a repository
            Items = Enumerable
                .Range(1, 5)
                .Select(i => new Item 
                { 
                    Value = i, 
                    Text = "item " + i 
                })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // TODO: based on the value of model.SelectedItemValue 
        // you could perform some action here
        return RedirectToAction("Index");
    }
}

and finally the strongly typed view:

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