EditorFor - 传递到字典中的模型项是'System.Int32'类型,但是这个字典需要一个'System.String'类型的模型项

use*_*286 10 asp.net-mvc-2

我似乎无法弄清楚为什么这不起作用.我正在使用ASP.NET MVC2,我只是试图通过将此代码放入/Shared/EditorTemplates/String.ascx来覆盖默认的编辑器外观:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%=Html.TextBox(null, Model, new { @class="Text" }) %>
Run Code Online (Sandbox Code Playgroud)

然后在我的View页面中,我有一行Int32类型:

<%: Html.EditorFor(model => model.AppID) %>
Run Code Online (Sandbox Code Playgroud)

出于某种原因,这会导致错误:

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.
Run Code Online (Sandbox Code Playgroud)

我不知道我的结果有什么不对,这很简单.如果类型是Int32,为什么它会尝试使用编辑器作为字符串?我还应该提一下,我已经覆盖了编辑器的一个bool?type(将布尔值渲染为复选框),它在同一页面上运行正常.

编辑

好吧,我搜索了很多次,但直到我在"相关"链接中找到它,我才看到这个帖子.我想这会起作用,但我仍然认为这是一个令人困惑和不一致的实现:

Asp.net Mvc显示String的模板,但现在每个简单类型都想使用它!

小智 5

在kendo ui Grid中:

public class BookBean
    {
        [ScaffoldColumn(false)]
        public Int32 Id { set; get; }

        public String Title { set; get; }

        public String Author { set; get; }

        public String Publisher { set; get; }

        [UIHint("Integer")]
        public Int32 Price { set; get; }

        [UIHint("Integer")]
        public Int32 Instore { set; get; }

        [UIHint("Integer")]
        public Int32 GroupId { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

在Shared/EditorTemplate文件夹中的Integer.ascx中执行:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %>

<%: Html.Kendo().IntegerTextBoxFor(m => m)
      .HtmlAttributes(new { style = "width:100%" })
      .Min(int.MinValue)
      .Max(int.MaxValue)
%>
Run Code Online (Sandbox Code Playgroud)


Jam*_*xon 1

在您的编辑器模板中,您已经告诉它需要 aViewUserControl<string>但您将 an 传递int给您的EditorFor.

由于编辑器模板正在等待 a string,而您传入​​了 a int,因此它无法工作。