【发布时间】:2017-05-12 11:38:33
【问题描述】:
所以以前这个视图只绑定到 一个 模型,但现在我正在使用:
@model Tuple<Namespace.Models.Model1, Namespace.Models.Model2>
所以现在当我绑定时:
@Html.HiddenFor(p => p.Id);
编译器不知道使用哪个模型,结果抛出这个错误:
The type arguments for method 'InputExtensions.HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
我尝试使用以下方法显式推断模型:
@Html.HiddenFor(Model1 => Model1.Id);
但我明白了:
The type arguments for method 'InputExtensions.HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
所以我尝试以下方法:
@Html.HiddenFor<TModel, TProperty>(HtmlHelper<TModel> p => p.Id);
Id 属性在 Model1 中定义。
并得到一大堆其他错误。在网上广泛阅读后,我仍然没有找到解决方案。我该如何解决这个问题?
【问题讨论】:
-
不要使用
Tuple来生成表单控件——提交时不能绑定——Tuple没有无参数构造函数。使用视图模型。 -
@Stephen Muecke 例子?
-
What is ViewModel in MVC?。创建一个视图模型,其中包含要在视图中显示/编辑的
Model1和Model2的属性。
标签: asp.net-mvc razor