0

我已将模型中的字符串属性转换为具有与我的自定义类型之间的隐式运算符的类EngNum。我这样做是为了让这种类型的所有出现都有我的自定义编辑器,即使该类型应该像字符串一样表现和使用。

我的问题是该属性不再正确绑定到我的模型,即使该值存在于 POST 的表单中。

请参阅下面的我的EngNum类型:

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

现在它显示在视图中:

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

这是它的编辑器:

<%@ 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) %>

ComboBoxForTextBoxFor与一些 jquery 一起呈现 a 。

在我从视图中获取此代码并将其放入编辑器之前,它运行良好。

我必须做些什么才能在回发时正确绑定我的财产?


控制器动作中的有以下与属性ModelState关联的异常:EngineerNumber

{System.InvalidOperationException:从类型“System.String”到类型“ThreeSixtyScheduling.Models.EngNum”的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。在 System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo 文化,对象值,类型 destinationType) 在 System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo 文化,对象值,类型 destinationType) 在 System.Web.Mvc.ValueProviderResult.ConvertTo (类型类型,CultureInfo 文化)在 System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}


控制器方法(和模型类型):

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

代码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);
    }
4

2 回答 2

1

您需要通过TypeConverterAttribute. ASP.NET MVC 将使用此类型转换器将字符串转换为您的类型。

于 2014-11-28T22:08:55.047 回答
1

模型绑定器不会调用您的自定义隐式运算符。您需要拥有同名的公共属性或编写自定义模型绑定器。通常您不需要在视图模型上使用隐式运算符。

于 2012-08-23T12:57:18.670 回答