视图支持继承吗?

发布于 2024-11-08 19:02:37 字数 356 浏览 0 评论 0原文

我基本上有一个模型,它由另一个模型组成,该模型是一个带有 2 个子类的基类。在我看来,我将如何使用它。显然,主模型将被传递到视图中;但是我如何将子模型转换为它的子类之一?请参阅以下示例来了解我的意思。在我看来,我会拥有 TransporationModel。假设我们正在从表单中收集该模型的数据,当前表单正在获取 Car 类型 Vehicle 的详细信息。提交表单后,我如何将汽车转换为其基本类型车辆?

public class TransaportationModel
{
    public VehicleModle vehicle { get; set; }
    ...other fields
}

非常感谢, 詹姆斯

I basically have a model that consists of another model that is a base class with 2 sub classes. How would I use this in my view. Obviously the main model would be passed into the view; however how would I cast the the submodel to one of it's subsclassesclasses? Please see the following for an example of what I mean. In my view I would have the TransporationModel. Say we are collecting data for this model from a form, and the current form is getting details for a Car type Vehicle. On submitting the form how would I cast the Car to its base type Vehicle?

public class TransaportationModel
{
    public VehicleModle vehicle { get; set; }
    ...other fields
}

Many thanks,
James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2024-11-15 19:02:37
Car car = new Car { /*Properties*/ };
Vehicle vehicle = (car as Vehicle);

来自维基百科:
“模型管理应用程序域的行为和数据,响应有关其状态的信息请求(通常来自视图),并响应更改状态的指令(通常来自控制器)。在事件驱动的系统中,模型当信息发生变化时通知观察者(通常是视图),以便他们做出反应。”

这意味着管理数据的各个类本身并不是“模型”。所有这些类一起创建了模型。您应该更多地研究 MVC 模式和对象思维。

Car car = new Car { /*Properties*/ };
Vehicle vehicle = (car as Vehicle);

From Wikipedia:
"The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react."

This means the individual classes that manage your data aren't 'models' perse. It is all of these classes together that create the model. You should look more into MVC pattern and thinking in objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文