相关疑难解决方法(0)

ViewBag名称可以与DropDownList中的Model属性名称相同吗?

我正在研究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

11
推荐指数
1
解决办法
1339
查看次数

MVC5 Razor html.dropdownlistfor当值在数组中时选择

我正在使用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)

c# asp.net-mvc razor

9
推荐指数
1
解决办法
9357
查看次数

具有键"XXX"的ViewData项的类型为"System.String",但必须是"IEnumerable <SelectListItem>"类型

对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)

asp.net-mvc razor

7
推荐指数
1
解决办法
1万
查看次数