I'm creating user control in MVC3 application. My view model looks like that:
public class MyViewModel
{
public object Value { get; set; }
}
The Value property could be an int, string or bool so I can't use [DataType]
attribute.
When I create my view model:
var viewModel = new MyViewModel { Value = "" };
or
var viewModel = new MyViewModel { Value = 1 };
I assume that this code:
<%: Html.EditorFor(p => p.Value) %>
should render an HTML input of type textbox. Unfortunately nothing is being rendered.
Everything works fine when I use bool value or some not empty string. Here's an example:
var viewModel = new MyViewModel { Value = true };
Html.EditorFor
renders checkbox input:
I did some research, but for now I didn't found solution.
Not a direct answer, but can't you just make your ViewModel generic:
public class MyViewModel<T>
{
public T Value { get; set; }
}
This way, the Html helper method can resolve at compile time exactly what type value is, and exactly which editor to render.
I used:
<%: Html.Editor("Value") %>
instead of:
<%: Html.EditorFor(p => p.Value) %>
and everything works great! The textbox is being rendered for empty string or int value. What is wrong with expression p => p.Value
?
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.