模型绑定 - 输入外部组件
我在程序集中有一个类型,该类型未由核心库引用,但由 Web 应用程序引用。例如
namespace MyApp.Models {
public class LatestPosts {
public int NumPosts { get; set; }
}
}
,现在我在核心库中有以下代码:
[HttpPost, ValidateAntiForgeryToken]
public ActionResult NewWidget(FormCollection collection) {
var activator = Activator.CreateInstance("AssemblyName", "MyApp.Models.LatestPosts");
var latestPosts = activator.Unwrap();
// Try and update the model
TryUpdateModel(latestPosts);
}
该代码非常不言自明,但latestPosts.NumPosts属性永远不会更新,即使该值存在于表单集合中。
如果有人能帮助解释为什么这不起作用以及是否有其他方法,我将不胜感激。
谢谢
I have a type in an assembly which isn't referenced by the core library but is referenced from the web application. e.g.
namespace MyApp.Models {
public class LatestPosts {
public int NumPosts { get; set; }
}
}
Now i have the following code in the core library:
[HttpPost, ValidateAntiForgeryToken]
public ActionResult NewWidget(FormCollection collection) {
var activator = Activator.CreateInstance("AssemblyName", "MyApp.Models.LatestPosts");
var latestPosts = activator.Unwrap();
// Try and update the model
TryUpdateModel(latestPosts);
}
The code is quite self explanatory but latestPosts.NumPosts property never updates even though the value exists in the form collection.
I'd appreciate it if someone could help explain why this does not work and whether there is an alternative method.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与该类型位于另一个程序集中或您使用 Activator.Create 动态创建它这一事实无关。以下代码以非常简化的方式说明了该问题:
问题源于
Controller.TryUpdateModel
使用typeof(TModel)
而不是model .GetType()
来确定模型类型,如 此连接问题(已关闭,原因是:按设计
)。解决方法是滚动您的自定义
TryUpdateModel
方法,该方法的行为将符合您的预期:然后:
Your problem has nothing to do with the fact that the type is in another assembly or that you are dynamically creating it with
Activator.Create
. The following code illustrates the issue in a much simplified way:The problem stems from the fact that
Controller.TryUpdateModel<TModel>
usestypeof(TModel)
instead ofmodel.GetType()
to determine the model type as explained in this connect issue (which is closed with the reason:by design
).The workaround is to roll your custom
TryUpdateModel
method which will behave as you would expect:and then: