Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:55:51+00:00 2026-06-10T04:55:51+00:00

I’ve converted a string property in my model to a class that has implicit

  • 0

I’ve converted a string property in my model to a class that has implicit operators to and from my custom type EngNum. I did this so that all occurences of this type would have my custom editor, even though the type should behave and be used like a string.

My problem is that the property is no-longer bound correctly to my model even though the value is there in the Form on POST.

See below for my EngNum type:

public class EngNum
{
    private string internalString;

    public EngNum() { }

    public EngNum(string number)
    {
        internalString = number;
    }

    public static implicit operator string(EngNumnumber)
    {
        return number == null ? null : number.internalString;
    }

    public static implicit operator EngNum(string number)
    {
        return new EngineerNumber() { internalString = number };
    }
}

And here’s now its displayed in the view:

<%= Html.EditorFor(m => m.EngineerNumber) %>

And here’s the editor for it:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ThreeSixtyScheduling.Models.EngineerNumber>" %>
<%@ Import Namespace="ThreeSixtyScheduling.BLL.Utilities" %>
<%= Html.ComboBoxFor(m => m,
                     new { @class = "EngineerNumber" },
                     Url.Action("MasternautEngineers", "Data", new { area = (string)null }),
                     Model, 0) %>

ComboBoxFor renders a TextBoxFor along with some jquery.

Before I took this code from the view and put it into the editor it worked fine.

What do I have to do to get my property bound properly on postback?


The ModelState in the controller action has the following exception associated with the EngineerNumber property:

{System.InvalidOperationException: The parameter conversion from type
‘System.String’ to type ‘ThreeSixtyScheduling.Models.EngNum’ failed
because no type converter can convert between these types. at
System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo
culture, Object value, Type destinationType) at
System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo
culture, Object value, Type destinationType) at
System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo
culture) at
System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary
modelState, String modelStateKey, ValueProviderResult
valueProviderResult, Type destinationType)}


The controller method (and type of model):

[HttpPost]
public ActionResult CreateStockcheckJob(CreateStockcheckJobModel viewModel)

public class CreateStockcheckJobModel
{
    [Required]
    [DisplayName("Engineer Number")]
    public EngNum EngineerNumber { get; set; }

    [Required]
    [DisplayName("Date and Time")]
    public DateTime DateAndTime { get; set; }

    public bool JobCreated { get; set; }

    public CreateStockcheckJobModel()
    {
        DateAndTime = DateTime.Today.WithTimeOfDay(8, 0, 0);
    }
}

Code for the ComboBoxFor:

    public static MvcHtmlString ComboBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                               Expression<Func<TModel, TProperty>> expression,
                                                               object htmlProperties,
                                                               string ajaxJSONLocation,
                                                               string selectedValue,
                                                               int minLength)
    {
        return ComboBoxFor(htmlHelper, expression, htmlProperties, ajaxJSONLocation, selectedValue, minLength, false, string.Empty);
    }

    public static MvcHtmlString ComboBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                               Expression<Func<TModel, TProperty>> expression,
                                                               object htmlProperties,
                                                               string ajaxJSONLocation,
                                                               string selectedValue,
                                                               int minLength,
                                                               bool hideId,
                                                               string selectCallbackScript)
    {
        var textboxHTML = htmlHelper.TextBoxFor(expression, htmlProperties);

        var scriptString = @"<script type=""text/javascript"">
$(function() {
    " + (string.IsNullOrEmpty(selectedValue) ? "$('#" + htmlHelper.IdFor(expression) + @"').val('')" : string.Empty) + @"
    $.getJSON('" + ajaxJSONLocation + @"', function(result) {

        $('#" + htmlHelper.IdFor(expression) + @"').autocomplete({
                                                                     minLength: " + minLength.ToString() + @",
                                                                     source: function(request, response) {
                                                                                 dataArray = new Array();
                                                                                 $.each(result, function(k, v) {
                                                                                    if (v.value.toUpperCase().indexOf(request.term.toUpperCase()) != -1 ||
                                                                                        v.desc.toUpperCase().indexOf(request.term.toUpperCase()) != -1) {
                                                                                        dataArray.push(v);
                                                                                    }
                                                                                 });
                                                                                 response(dataArray);
                                                                     },
                                                                     focus: function(event, ui) {},
                                                                     select: function(event, ui) {
                                                                     $('#" + htmlHelper.IdFor(expression) + @"').val( ui.item.value );
                                                                     " + selectCallbackScript + @"
                                                                     return false; }
                                                            })
                                                   .data(""autocomplete"")._renderItem = function (ul, item) {
                                                        return $(""<li></li>"")
                                                                .data(""item.autocomplete"", item)
                                                                .append(""<a>"" + " + (hideId ? string.Empty : @"item.value + ""<br/>"" + ") + @"""<span>"" + item.desc + ""</span></a>"")
                                                                .appendTo(ul);
                                                   };
    });
});
</script>";

        return MvcHtmlString.Create(textboxHTML.ToString() + scriptString);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:55:53+00:00Added an answer on June 10, 2026 at 4:55 am

    The model binder is not going to call your custom implicit operator. You need to have a public property with the same name or write a custom model binder. Usually you don’t need to use implicit operator on view models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.