我正在研究ASP.NET MVC-4 Web应用程序.我在我的action方法中定义了以下内容来构建一个SelectList:
ViewBag.CustomerID = new SelectList(db.CustomerSyncs, "CustomerID", "Name");
Run Code Online (Sandbox Code Playgroud)
然后我在我的DropDownListFor内部渲染我View:
@Html.DropDownListFor(model => model.CustomerID, (SelectList)ViewBag.CustomerID, "please select")
Run Code Online (Sandbox Code Playgroud)
如图所示,我将ViewBag属性命名为等于Model属性名称CustomerID.从我自己的测试中,定义相同的名称不会导致任何问题或冲突,但我应该避免这种情况吗?
asp.net asp.net-mvc html-helper html.dropdownlistfor asp.net-mvc-4
我正在使用C#和.NET Framework 4.6.1开发ASP.NET MVC 5应用程序.
我有这个View:
@model MyProject.Web.API.Models.AggregationLevelConfViewModel
[...]
@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, (SelectList)Model.HelperCodeTypeItems, new { id = "Configurations[0].HelperCodeType" })
Run Code Online (Sandbox Code Playgroud)
该ViewModel方法是:
public class AggregationLevelConfViewModel
{
private readonly List<GenericIdNameType> codeTypes;
private readonly List<GenericIdNameType> helperCodeTypes;
public IEnumerable<SelectListItem> CodeTypeItems
{
get { return new SelectList(codeTypes, "Id", "Name"); }
}
public IEnumerable<SelectListItem> HelperCodeTypeItems
{
get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}
public int ProductionOrderId { get; set; }
public string ProductionOrderName { get; set; }
public IList<Models.AggregationLevelConfiguration> Configurations { …Run Code Online (Sandbox Code Playgroud) 对MVC来说很新.尝试提交包含静态DropDownListFor的表单时,我收到以下异常.
The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The ViewData item that has the key 'CurrentPosition' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
Source Error:
Line 36: @Html.LabelFor(m => m.CurrentPosition, new { …Run Code Online (Sandbox Code Playgroud)