Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorFor - The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'

I can't seem to figure out why this wouldn't work. I'm using ASP.NET MVC2 and I'm simply trying to override the default editor appearance by placing this code into /Shared/EditorTemplates/String.ascx:

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

Then in my View page, I have this line which is of type Int32:

<%: Html.EditorFor(model => model.AppID) %>

For some reason, this results in the error:

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'.

I don't see how anything could be wrong on my end, it's pretty simple. Why does it try to use the editor for a string if the type is an Int32? I also should mention that I've overridden the Editor for a bool? type (to render boolean values as a checkbox) and it works just fine on the same page.

EDIT

Well I searched many times, but I didn't see this post until I found it in the "Related" links. I suppose this will work, I still think it's a confusing and inconsistent implementation though:

Asp.net Mvc Display template of String, but now every simple type wants to use it!

like image 208
user949286 Avatar asked Sep 16 '11 16:09

user949286


2 Answers

in kendo ui Grid do :

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; }
    }

in Integer.ascx in Shared/EditorTemplate folder do :

<%@ 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)
%>
like image 179
Ehsan Gholami Avatar answered Sep 30 '22 16:09

Ehsan Gholami


In your editor template you've told it to expect a ViewUserControl<string> but you're passing an int to your EditorFor.

Since the editor template is waiting for a string, and you're passing in an int, it wont work.

like image 25
Jamie Dixon Avatar answered Sep 30 '22 17:09

Jamie Dixon