MVC 控制器独立于视图类型 UpdateModel
我想在不通知视图类型的控制器中使用 updateModel 。 我有不同的视图,它们具有不同的类型,但都有一个ExternalBase 类作为继承类型。
因此,在我的控制器中,我总是有一个ExternalBase,但控制器不知道正确的类型。
保存时,我调用一个方法来获取正确的对象,但它返回该对象作为 externalBase。 内部类型是我的正确类型。 如果我将鼠标悬停在对象上,它就是调用保存的视图类型。 现在,如果我对其调用 updateModel,它不会填充属性。
举个例子:
// MyExternalBase 是一个空类
The class person
public class Person
: MyExternalBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<TheParameters> Parameters { get; set; }
public Address Address { get; set; }
public TheParameters[] OtherParameters { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public TheParameters Parameter { get; set; }
}
public class TheParameters
{
public string Parameter { get; set; }
}
在我的控制器中保存时我执行以下操作:
MyExternalBase p = new Person();
UpdateModel(p, "Person", form.ToValueProvider());
现在 p 没有填满。
如果我改用 Person p = new Person() 那么就没问题了。 但我希望我的控制器独立于视图类型。
这是 updateModel 中的错误还是不可能的事情? 或者有解决方法吗?
I want to use the updateModel in a controller that has no notice of the type of the view.
I have different views that have different types but all have an ExternalBase class as inherited type.
So in my controller I always have a ExternalBase but the controller doesn't know the correct type.
On saving I call a method that gets the correct object but it returns this as an externalBase. The innertype is my correct type. If I hover over my object it is the type of the view that calls the save. Now if I call the updateModel on it it doesn't fill in the properties.
As an example:
// MyExternalBase is an empty class
The class person
public class Person
: MyExternalBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<TheParameters> Parameters { get; set; }
public Address Address { get; set; }
public TheParameters[] OtherParameters { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public TheParameters Parameter { get; set; }
}
public class TheParameters
{
public string Parameter { get; set; }
}
In my controller on the Save I do the following:
MyExternalBase p = new Person();
UpdateModel(p, "Person", form.ToValueProvider());
Now the p doesn't fill up.
If I instead use Person p = new Person() Then it is no problem. But I want my controller to be independent of the view type.
Is this an error in the updateModel or something that just isn't possible?
Or is ther a workaround for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 .NET 4.0,请尝试使用:
在这种情况下,UpdateModel 将正常工作。 在处理单个控制器和视图时,我也遇到了这个问题,我想将其用于所有继承自同一类型的多个对象。
If you're using .NET 4.0, then try using:
In that situation, UpdateModel will work properly. I ran into this as well when working on a single controller and view that I want to use for several objects that all inherit from the same type.
我也想知道这个问题的答案,在使用传递基类时我遇到了一个非常相似的问题,即使我可以安全地向下转换为正确的类型,并且所有数据都完好无损。
UpdateModel 似乎看不到内部属性,除非我将基类型向下转换为正确的类型,但这涉及一个 switch 语句来获取正确的类型。
I too would like to know the answer to this, I have a very simular problem when using passing a base class even though I can safely downcast to the correct type with all the data in-tact.
UpdateModel doesn't seem to see the inner properties unless I downcast the basetype to the correct type, but this involves a switch statement to get the correct type.