ASP.NET MVC3 Html.EditorFor和类型的对象属性 [英] ASP.NET MVC3 Html.EditorFor and property of type object
问题描述
我创建的应用程序MVC3用户控件。我的视图模型看起来像:
I'm creating user control in MVC3 application. My view model looks like that:
public class MyViewModel
{
public object Value { get; set; }
}
Value属性可能是int,字符串或布尔所以我不能使用 [数据类型]
属性。
当我创建视图模型:
var viewModel = new MyViewModel { Value = "" };
或
var viewModel = new MyViewModel { Value = 1 };
我认为这code:
I assume that this code:
<%: Html.EditorFor(p => p.Value) %>
应该呈现类型文本框的HTML输入。不幸的是没有被渲染。
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
渲染复选框,输入:
我做了一些研究,但现在我没有找到解决办法。
I did some research, but for now I didn't found solution.
推荐答案
我用:
<%: Html.Editor("Value") %>
而不是:
<%: Html.EditorFor(p => p.Value) %>
和一切的伟大工程!文本框被渲染为空字符串或int值。什么是错的前pression P =&GT; 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
?
这篇关于ASP.NET MVC3 Html.EditorFor和类型的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!