c# - ASP.NET返回多个变量查看

标签 c# asp.net .net asp.net-mvc asp.net-mvc-4

我无法弄清楚如何将多个变量返回到一个 View 。像这样的东西。我能得到一点帮助吗?

    public ActionResult CheatSheet()
    {
        var var1 = from ts in db.thisdatabase
                   select ts;

        var var2 = from ls in db.thisdatabase
                   select ls;

        var var3 = from d in db.thisdatabase
                   select d;

        return View(var1,var2,var3);
    }

最佳答案

考虑使用 ViewModel

您需要使用 ViewModel 来组合所有这些不同的结果并将该模型传递给 View :

public class ExampleViewModel
{
      // Example collections for each of your types
      public IEnumerable<Something> CollectionA { get; set; }
      public IEnumerable<Something> CollectionB { get; set; }
      public IEnumerable<Something> CollectionC { get; set; }
}

然后只需使用类似于以下代码的内容来执行您的特定查询,并使用这些查询的结果来构建您的模型,然后将其传递给 View :

// Build the model
var model = new ExampleViewModel(){

     // You'll likely want a .ToList() after these to ensure things work as expected
     CollectionA = db.thisdatabase.Select(x => x.ts),
     CollectionB = db.thisdatabase.Select(x => x.ts),
     CollectionC = db.thisdatabase.Select(x => x.ts),
};

// Pass it to your View
return View(model);

Note: This assumes that you aren't actually querying the same exact table with each of your queries. If that was the case, then it may be more efficient to pull back a single collection with each of your properties and then assign your individual collections of properties to the model (instead of performing multiple, possibly redundant, queries.

然后在 View 中,您可以按预期引用底层属性集合并遍历它们或执行任何其他类型的操作:

@model YourProject.ViewModels.ExampleViewModel

@foreach (var item in Model.CollectionA) { ... }
@foreach (var item in Model.CollectionB) { ... }
@foreach (var item in Model.CollectionC) { ... }

对于更复杂的场景

如果您不想简单地访问数据库中的单个列,而是访问多个列,您可能希望创建另一个模型/类来映射您的属性,然后将这些属性的实例存储在您的 ViewModel 中。

让我们看看您的示例,看看它是如何工作的。因此,您目前正在寻找存储 tslsd 属性的方法,因此让我们创建一个类来将它们存储在:

public class Example
{
     public string Ts { get; set; }
     public string Ls { get; set; }
     public string D { get; set; }
}

现在,当您执行查询时,只需获取所有这些并将它们映射到 Select() 调用中:

// This will now be an IEnumerable<Example>
var models = db.thisdatabase.Select(x => new Example()
{
    Ts = x.ts,
    Ls = x.ls,
    D = d
});

如果这就是您所需要的,您现在可以将其直接传递给您的 View :

// If you did this, you'd need to adjust your @model declaration
return View(model);

或者,如果您需要为不同的表构建不同的模型,然后将所有这些集合组合到类似于原始示例的 ViewModel 中,您可以执行多个操作:

var model = new ExampleViewModel(){
     CollectionA = db.thisdatabase.Select(x => new Example(x)),
     CollectionB = db.othertable.Select(x => new OtherExample(x)),
     CollectionC = db.yetanother.Select(x => new LastExample(x))
};

其他存储形式

根据您的需要,您可以考虑其他几种方法,例如使用 ViewBag 集合。 ViewBag 是一个动态集合,可让您轻松地在 View 中存储和访问对象:

ViewBag.A = db.thisdatabase.Select(x => x.ts),
ViewBag.B = db.thisdatabase.Select(x => x.ts),
ViewBag.C = db.thisdatabase.Select(x => x.ts),

return View();

这基本上将以相同的方式工作,但是您将只使用 @ViewBag 而不是在您的 View 中引用 @Model。还值得注意的是,这也可以与实际模型结合使用。

还有其他方法,例如使用 ViewDataSession 集合,但您真的应该使用模型。它更符合实际的 MVC 模式,如果你习惯了它,它应该会让你的生活更轻松。

关于c# - ASP.NET返回多个变量查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43791585/

相关文章:

c# - 使用 JSON.NET 库覆盖 WriteJson

c# - Entity Framework linq 查询何时返回动态代理类型,何时不返回?

c# - 枚举 C# DalSoft.RestClient 和/或 Json.net 中的 JSON 属性

.net - .NET 和 ASP.NET 有什么区别

asp.net - 通过 Set-Cookie 设置 cookie 的尝试被阻止,因为它具有 "Secure"属性

.net - Windows 应用程序图标文件

c# - 在 Dictionary<string, Dictionary<string, int>> 上进行 linq 查询时出现问题

c# - Linq 快速相交查询 - 增强?

.net - 可以使用整数数组作为唯一的字典键吗?

javascript - 如何将 JavaScript 变量传递给服务器端方法