MVC3 选定值

发布于 2024-12-04 07:40:19 字数 746 浏览 0 评论 0原文

在我的模型中:

public SelectList QuestionGroupSelectList { get; set; }

------
List<QuestionGroup> questionGroupList = questionGroupRepository.GetQuestionGroup_BySurveyId(survey.Id);

Dictionary<int, string> questionGroupDictionary = questionGroupList.ToDictionary(l => l.Id, l => l.Name);

QuestionGroupSelectList = new SelectList(questionGroupDictionary, "key", "value", questionGroupId);





---------------------------------------
In view:
@Html.DropDownList("QuestionGroupSelectList", Model.QuestionGroupSelectList, "Choose Here")

当我调试时,我在 QuestionGroupSelectList 中得到 2 项(一项 Id 30,一项 Id 35),它说 selectedValue 是 35 (questionGroupId = 35)

但 selectedvalue 在视图中不起作用,任何想法?

提前致谢!

In my model:

public SelectList QuestionGroupSelectList { get; set; }

------
List<QuestionGroup> questionGroupList = questionGroupRepository.GetQuestionGroup_BySurveyId(survey.Id);

Dictionary<int, string> questionGroupDictionary = questionGroupList.ToDictionary(l => l.Id, l => l.Name);

QuestionGroupSelectList = new SelectList(questionGroupDictionary, "key", "value", questionGroupId);





---------------------------------------
In view:
@Html.DropDownList("QuestionGroupSelectList", Model.QuestionGroupSelectList, "Choose Here")

When i debug i get 2 items in QuestionGroupSelectList (one with Id 30 and one with Id 35), and it says that the selectedValue is 35 (questionGroupId = 35)

But the selectedvalue does not work in the view, any ideas?

Thanks in advance!

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

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

发布评论

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

评论(1

暗恋未遂 2024-12-11 07:40:19

您应该使用不同的属性来绑定下拉列表值。此外,您还应该使用视图模型和强类型助手,如下所示:

public class MyViewModel
{
    public int QuestionGroupId { get; set; }
    public SelectList QuestionGroupSelectList { get; set; }
}

然后您可以有一个控制器操作来填充此视图模型并将其传递给视图:

public ActionResult Foo()
{ 
    // This collection could come from anywhere 
    // normally you will query a repository here to fetch those values
    var values = new[] 
    {
        new { Key = "1", Value = "item 1" },
        new { Key = "2", Value = "item 2" },
        new { Key = "3", Value = "item 3" },
    }

    var model = new MyViewModel
    {
        // preselect the second value
        QuestionGroupId = 2,
        QuestionGroupSelectList = new SelectList(values, "Key", "Value")
    }
    return View(model);
}

最后在您的视图中:

@model MyViewModel

@Html.DropDownListFor(
    x => x.QuestionGroupId, 
    Model.QuestionGroupSelectList, 
    "Choose Here"
)

You should use a different property to bind your dropdownlist value to. Also you should use view models and strongly typed helpers, like this:

public class MyViewModel
{
    public int QuestionGroupId { get; set; }
    public SelectList QuestionGroupSelectList { get; set; }
}

then you could have a controller action which populates this view model and passes it to the view:

public ActionResult Foo()
{ 
    // This collection could come from anywhere 
    // normally you will query a repository here to fetch those values
    var values = new[] 
    {
        new { Key = "1", Value = "item 1" },
        new { Key = "2", Value = "item 2" },
        new { Key = "3", Value = "item 3" },
    }

    var model = new MyViewModel
    {
        // preselect the second value
        QuestionGroupId = 2,
        QuestionGroupSelectList = new SelectList(values, "Key", "Value")
    }
    return View(model);
}

and finally in your view:

@model MyViewModel

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