c# - MVC 2.0 中的绑定(bind)下拉列表

标签 c# .net asp.net-mvc

我试图在 MVC 模型中绑定(bind)下拉菜单,但出现错误我的代码如下:

private IEnumerable<System.Web.Mvc.SelectListItem> _property;
public IEnumerable<System.Web.Mvc.SelectListItem> TestPapers
{
get
{
Controller _controller = new Controller();
_property = _controller.BindTestPaperDropdown(string.Empty);
return _property;
}
set
{
_property = value;
}

谁能说我哪里错了?

最佳答案

这也可以工作:

<select name="sdfsdf">
    @foreach selectOption in ViewBag['SelectOptions']
        <option value="@selectOption.Value">@selectOption.Text</option>
    @endforeach
</select>

只要把所有东西都放在 Controller 的 viewbag 里 (示例在 Razor 中)

对于 aspx,我想它会是这样的……但是我已经有一段时间没有写任何 aspx 了,所以我可能会遗漏一些东西,但这是主要思想

<select name="sdfsdf">
    <% foreach(var p in products) { %>
        <option value="<% p.Value %>"> <% p.Text %> </option>
    <% } %>
</select>

关于c# - MVC 2.0 中的绑定(bind)下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8429433/

相关文章:

C# 处理动态字体大小的方法

.net - 如何将文本转换为rtf,以便发送电子邮件?

c# - WriteAllBytes - 拒绝访问路径

c# - 在数据库中添加列然后更新 dbml

c# - 如何修复名称 'Name' & 'Value' 在当前上下文中不存在

asp.net-mvc - IIS 7.5。无法禁用 URL 重写规则(我删除了规则,但它们仍然影响)

asp.net mvc 5 投票系统

asp.net-mvc - @html.Label 和@html.LabelFor 的区别

javascript - Form.serialize() 似乎返回 null

c# - 具有可更改设置的单个 exe