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);
}
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.
You need to add a type converter to your class through TypeConverterAttribute
. This type converter will be used by ASP.NET MVC to convert strings to your type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.