문제

I have view model (used for filtering) with property:

int? Id {get;set;}

In my view I have sth like that:

@Html.EditorFor(m=>m.Id)

When I submit form I get error:

Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, 
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

I dont want to do sth like that:

@Html.TextBox("Model.Id", Model.Id.HasValue ? MOdelId.Value : 0)

When I checked in Fiddler there is parameter ID with proper vaue but I can't even debbug because it doesn't get into Action method.

Can you help me?

PS sorry for my english

도움이 되었습니까?

해결책

For the debugger to reach the breakpoint, it should first get pass the model binder. Model binder is the thing that magically binds values from your HTTP request to the model. All parsing will happen at the model binder and if it fails there, you will never hit the breakpoint in your action method.

Try the following on your view

@Html.EditorFor(m=>m.Id, new {name = ‘Id’, id=’Id’})

or

@Html.TextBoxFor(m=>m.Id)

Cheers

다른 팁

This is how my generated view looks like:

<input class="text-box single-line" id="Filter_Id" name="Filter.Id" type="text" value="">

PS: i'm using many other attributes (my datetimes are nullable too) and I get ths error only when I input value in this stupid int? ;/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top