ASP.NET MVC中的级联DropdownList [英] Cascading DropdownList in ASP.NET MVC

查看:67
本文介绍了ASP.NET MVC中的级联DropdownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET MVC中将值绑定到DropdownList?

How to Bind Values To DropdownList in ASP.NET MVC?

推荐答案

在行动中:



In Action :

public ActionResult Create()
    {
        var db = new DbDataContext();
        IEnumerable<SelectListItem> states = db.States.Select(p => new SelectListItem
        {
            Value = p.StateId.ToString(),
            Text = p.Name
        });
        ViewData["YourData"] = states;
        return View();
    } 







查看:






In View :

<div class="editor-field">
                <%: Html.DropDownListFor(model => model.StateId, (IEnumerable<SelectListItem>)ViewData["YourData"])%>
                <%: Html.ValidationMessageFor(model => model.StateId) %>
            </div>


首先,有没有像ASP.NET MVC中的数据绑定那样的东西。您必须使用视图模型或ViewBag(或ViewData,如果您仍使用MVC2)将控制器中的选择值传递给模型。这是一个很好的howto: http:// www。 mikesdotnetting.com/Article/128/Get-The-Drop-On-ASP.NET-MVC-DropDownLists [ ^ ],但Google可以为您提供更多信息。

现在有级联。

有几种方法,但这些方法很有趣:

1)如果你知道你的所有列表首先渲染你的页面,你可以将它们作为JS数组嵌入到视图中,并在客户端过滤它们 - 这不是动态的,可能是一个巨大的开销。

2)嵌入你的级联逻辑进入视图: MVC级联Ajax的简单实现下拉 [ ^ ],但我不推荐这个,因为它不遵循MVC概念。

3)使用Jquery,制作一个ajax回调来填充第二个(及以下) )列表。我找到了一个插件,您可以使用: http://weblogs.asp.net/rajbk/archive/2010/05/20/cascadingdropdown-jquery-plugin-for-asp-net-mvc.aspx [ ^ ] - 我推荐这种方法,因为你可以动态地计算要放入列表的元素,并且你不需要刷新页面。
First of all, there is no such thing as data binding in ASP.NET MVC. You have to pass select values from the controller to the model either using a view model, or via ViewBag (or ViewData if you still use MVC2). Here is a good "howto": http://www.mikesdotnetting.com/Article/128/Get-The-Drop-On-ASP.NET-MVC-DropDownLists[^], but google can give you more.
And now with cascading.
There are several approaches, but these can be interesting:
1) If you know all your lists on the first render of your page, you can embed them in the view as JS arrays, and filter them on client side - this is not dynamic and can be a huge overhead.
2) Embed your "cascading" logic into the view: Simple Implementation of MVC Cascading Ajax Drop Down[^], but I don't recommend this, because it is not following the MVC concepts.
3) Using a Jquery, making an ajax callback to populate the second (and following) lists. I found a plugin, you might use: http://weblogs.asp.net/rajbk/archive/2010/05/20/cascadingdropdown-jquery-plugin-for-asp-net-mvc.aspx[^] - I reccomend this approach, since you can dinamically calculate what elements to put into the list, and you don't need to refresh the page.


检查asp中级联下拉列表的清晰解决方案.net mvc:



http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net- mvc.aspx?CommentPosted = true#commentmessage [ ^ ]
check for clear solution of cascading dropdownlist in asp.net mvc:

http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx?CommentPosted=true#commentmessage[^]


这篇关于ASP.NET MVC中的级联DropdownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆