- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在一个项目中学习 MVC2、C# 和 Linq to Entities(是的,我很生气),但我在使用 DropDownListFor 并将 SelectList 传递给它时遇到了一些问题。
这是我 Controller 中的代码:
public ActionResult Create()
{
var Methods = te.Methods.Select(a => a);
List<SelectListItem> MethodList = new List<SelectListItem>();
foreach (Method me in Methods)
{
SelectListItem sli=new SelectListItem();
sli.Text = me.Description;
sli.Value = me.method_id.ToString();
MethodList.Add(sli);
}
ViewData["MethodList"] = MethodList.AsEnumerable();
Talkback tb = new Talkback();
return View(tb);
}
我在尝试让 DropDownListFor
获取 ViewData
中的 MethodList
时遇到了麻烦。当我尝试时:
<%:Html.DropDownListFor(model => model.method_id,new SelectList("MethodList","method_id","Description",Model.method_id)) %>
它出错并显示以下消息
DataBinding: 'System.Char' does not contain a property with the name 'method_id'.
我知道这是为什么,因为它将 MethodList
作为字符串,但我不知道如何让它接受 SelectList
。如果我使用普通的 DropDownList
执行以下操作:
<%: Html.DropDownList("MethodList") %>
对此很满意。
有人能帮忙吗?
最佳答案
编辑:那么您正在使用 Entity Framework ,是吗?在那种情况下,如果你在评论中添加了附加信息,你会想要做这样的事情:
public ActionResult Create()
{
var viewModel = new CreateViewModel(); // Strongly Typed View
using(Entities dataModel = new Entities()) // 'te' I assume is your data model
{
viewModel.Methods = dataModel.Methods.Select(x => new SelectListItem()
{
Text = x.Description,
Value = x.method_id.ToString()
});
}
return View(viewModel);
}
您的强类型 View 模型将是:
public class CreateViewModel
{
public string SelectedMethod { get; set; }
public IEnumerable<SelectListItem> Methods { get; set; }
}
您的 View 代码将是:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CreateViewModel>" %>
<%-- Note the Generic Type Argument to View Page! --%>
<%: Html.DropDownListFor(m => m.SelectedMethod, Model.Methods) %>
关于c# - ASP.Net MVC2 DropDownListFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749971/
我有一个 DropDownListFor 组件,它有一个从数据库获取值的读取操作。但是,我想根据另一个 DropDownListFor 的值过滤这些值。 这是具有过滤器值的 DropDownListF
我正在为语言生成一个下拉列表。我想将该列表的特定项目设为 selected。这是查看页面代码: @{var languageList = new List(); var cultures =
我有一个绑定(bind)到模型成员的 DropDownListFor 以及可供选择的项目列表。绑定(bind)到这个成员有效,但我似乎无法弄清楚的是如何在页面加载时显示模型的当前值。 查看 @Html
我已经看到许多关于 DropDownListFor 验证的类似问题和答案,但我还没有看到我的特定问题。 这是我的 html 助手。 VB: @Html.DropDownListFor(Function
我有一个带有 BootStrap 的 Html.DropDownListFor,它不像我表单中的 Html.TextBoxFor 那样左对齐。我试过“向左拉”和“向左文本”都没有运气。当我使用开发人员
我发誓我在尝试从静态状态列表填充 DropDownList 时遇到了麻烦。我更希望通过 EntityFramework 从数据库中预先填充此列表。 我的 RegisterViewModel publi
我在输入表单中有其他字段和下拉列表。他们都验证得很好。即使没有正确验证前端的两个元素仍然可以在帖子中正确地建模绑定(bind)。我不完全确定他们为什么不验证。这是设置: View 模型 public
我知道已经有很多类似的问题,但我花了几个小时试图解决这个问题,其他答案似乎都没有帮助! 我只想使用 MVC 在下拉列表中显示字符串列表。这真的有那么难吗?我没有“文本”和“值”分隔(尽管 MVC 似乎
我想在 HTML 助手中添加扩展方法来生成这样的选择和选项 Andorra United Arab Emirates Afghanistan 选项有一个数据域属性,我可以这样使用它 @Html.Dr
所以,我得到了一个在我看来看起来像这样的 DropdownListfor: @Html.DropDownListFor(m => m.ProductCategory, new SelectList(M
首先,我是 .NET MVC 的新手,虽然我以前使用过一般的 MVC 想法。我正在使用 .NET MVC4 和 CodeFirst 使用 Entity Framework 。 我正在尝试向“创建” V
我正在尝试将数据从我的数据库检索到 HTML,而不是检索到 Html.DropDownListFor,但我无法检索到标记。 NewCustomerViewModel public class
我有一个 departmentID 的下拉列表,它根据在 DepartmentCategoryID 中选择的内容进行填充,但是如果它留空,我将无法进行验证。它或所有其他的都有效,但这是以不同的方式完成
我想知道是否有人可以帮助我解决我面临的问题。我正在尝试使用 Razor 在 DropDownListFor 上创建搜索。 private List LoadStockitems() { Lis
我已经搜索了好几个小时并尝试了所有的建议。我想我缺少一些基本概念。我想使用模型而不是 ViewBag,并且我在回发的模型中设置了选定的项目/值。 我正在 cshtml 中设置一个 ddl 和 acti
这是我的 View 模型: public class EntityViewModel { [Required(ErrorMessage = "Title is required")]
我正在尝试填充 DropDownListFor数据库中的项目,但是当我尝试运行该程序时,它给了我错误 Object reference not set to an instance .就像方法(从数据
我有以下设置我的下拉菜单到荒谬的宽度: @Html.DropDownListFor(x => x.TypeId, new SelectList(Model.Type, "TypeId", "Ty
一直在尝试多种方法来更改 DropDownListFor 元素的样式。要么我做错了什么,要么有什么东西压倒了它。我已经尝试在 View 元素和 CSS 中更改它。但没有成功。 查看: @H
我显然在理解 JS 方面遇到了问题。我是新手,我正在尝试让它适用于以下用例: If "Healthcare" is selected from the "discipline" dropdown th
我是一名优秀的程序员,十分优秀!