Html.DisplayFor DropDownList 问题

发布于 2024-09-28 13:57:24 字数 824 浏览 2 评论 0原文

我使用 asp.net mvc 2。我有一个模型 Supermodel,由 2 个模型 TestModel1 和 TestModel2 组成。 在 SuperModelView 中,我做了以下事情:

 <%: Html.DisplayFor(x=> x.TestModel1, "TestModel1Template") %>

它工作得很好,除了下拉列表已填充但未设置选定值这一事实之外。 我在我的模板中使用以下代码作为下拉列表:

<%: Html.DropDownListFor(x=> x.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.Property1) %>

并且它没有设置所选属性。我将下面的代码放入 SuperModelView,调用 <%: Html.DisplayFor 填充模板,它工作得很好。所以我有点困惑,有什么区别?

<%: Html.DropDownListFor(x=> x.TestModel1.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.TestModel1.Property1) %>

更新:我试图调查这个问题,但有些事情是完全错误的。我可以共享整个代码,但不确定将其放在哪里,放在这里或附加单独的文件。

@Darin,我应该共享哪些其他部分,或者只是共享整个模型视图和控制器文件?

Im using asp.net mvc 2. I have a model Supermodel that consists of 2 models TestModel1 and TestModel2.
In SuperModelView Im doing the following thing:

 <%: Html.DisplayFor(x=> x.TestModel1, "TestModel1Template") %>

Its working just fine, except for the fact, that dropdownlist is populated but selected value is not set.
Im using the following code for a dropdownlist in my template:

<%: Html.DropDownListFor(x=> x.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.Property1) %>

and its not setting the selected property. I put the code below to SuperModelView, that calls <%: Html.DisplayFor
To populate the template and it works just fine. So I`m kinda puzzled, what is the difference?

<%: Html.DropDownListFor(x=> x.TestModel1.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.TestModel1.Property1) %>

UPDATE: I`ve tried to investigate the issue, but something is totally wrong. I can share the whole code, not sure where to put it, here or attach with separate files.

@Darin, what other parts should I share, or just share the whole model view and controller files?

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

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

发布评论

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

评论(1

彻夜缠绵 2024-10-05 13:57:24

首先显示模板只是为了显示。如果您需要使用下拉菜单进行编辑,请使用编辑器模板:

<%: Html.EditorFor(x => x.TestModel1, "TestModel1Template") %>

并在编辑器模板中:

<%: Html.DropDownListFor(x => x.Property1, Model.MyDDLList) %>

其中 MyDDLList 定义如下:

public IEnumerable<SelectListItem> MyDDLList { get; set; }

并在控制器操作中填写值:

public ActionResult Foo() 
{
    var model = new SuperViewModel 
    {
        TestModel1 = new TestModel1
        {
            // Set some selected value
            Property1 = "1",

            // Fill the drop down values
            // TODO: use a repository
            MyDDLList = new SelectList(new[]
            {
                new SelectListItem { Value = "1", Text = "text 1" },
                new SelectListItem { Value = "2", Text = "text 2" },
                new SelectListItem { Value = "3", Text = "text 3" },
            }, "Value", "Text")
        }
    }
    return View(model);
}

更新:

这是一个完整的工作示例:

模型:

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

控制器:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // Preselect the second item
            SelectedItemId = "2",
            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")
        };
        return View(model);
    }
}

视图(~/Views/Index.aspx):

<%: Html.DisplayForModel() %>

DisplayTemplate (~/Views/DisplayTemplates/MyViewModel):

<%: Html.DropDownListFor(x => x.SelectedItemId, Model.Items) %>

Firstly display templates are just for displaying. If you need to edit with drop down use editor template:

<%: Html.EditorFor(x => x.TestModel1, "TestModel1Template") %>

and in your editor template:

<%: Html.DropDownListFor(x => x.Property1, Model.MyDDLList) %>

where MyDDLList is defined like:

public IEnumerable<SelectListItem> MyDDLList { get; set; }

and in your controller action you fill the values:

public ActionResult Foo() 
{
    var model = new SuperViewModel 
    {
        TestModel1 = new TestModel1
        {
            // Set some selected value
            Property1 = "1",

            // Fill the drop down values
            // TODO: use a repository
            MyDDLList = new SelectList(new[]
            {
                new SelectListItem { Value = "1", Text = "text 1" },
                new SelectListItem { Value = "2", Text = "text 2" },
                new SelectListItem { Value = "3", Text = "text 3" },
            }, "Value", "Text")
        }
    }
    return View(model);
}

UPDATE:

Here's a complete working example:

Model:

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

Controller:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // Preselect the second item
            SelectedItemId = "2",
            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")
        };
        return View(model);
    }
}

View (~/Views/Index.aspx):

<%: Html.DisplayForModel() %>

DisplayTemplate (~/Views/DisplayTemplates/MyViewModel):

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