gpt4 book ai didi

asp.net-mvc - 如何在强类型 View 中包含多个模型对象?

转载 作者:行者123 更新时间:2023-12-04 19:33:41 24 4
gpt4 key购买 nike

当我从 ActionResult 函数创建强类型 View 时,用于创建 View 的 Visual Studio 对话框只允许我包含一个模型对象。

如何包含超过 1 个以便我可以对所有它们使用智能感知?

最佳答案

这不可能。您应该创建另一个模型来包装这两个模型。

例子:

//Model

public class FooModel1
{
public string Property {get;set;}
}

public class FooModel2
{
public string Property {get;set;}
}

public class FooModel
{
public FooModel1 One {get;set;}
public FooModel2 Two {get;set;}
}

// Controller :

public ActionResult Index()
{
var model = new FooModel() { One = new FooModel1 (), Two = new FooModel2 ()};
return View(model);
}

// View

@model FooModel
@Html.TextBoxFor(m=>m.One.Property)
@Html.TextBoxFor(m=>m.Two.Property)

关于asp.net-mvc - 如何在强类型 View 中包含多个模型对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17621485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com