ASP.NET返回多个变量以查看

Ale*_*x D 4 .net c# asp.net asp.net-mvc asp.net-mvc-4

我无法弄清楚如何将多个变量返回到视图.像这样的东西.我可以得到一些帮助吗?

    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);
    }
Run Code Online (Sandbox Code Playgroud)

Rio*_*ams 5

考虑使用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; }
}
Run Code Online (Sandbox Code Playgroud)

然后使用类似以下代码的内容来执行特定查询,并使用这些查询的结果来构建模型,然后将其传递给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);
Run Code Online (Sandbox Code Playgroud)

注意:这假设您实际上并未使用每个查询查询相同的表.如果是这种情况,那么用每个属性拉回单个集合然后将单独的属性集合分配给模型(而不是执行多个,可能是冗余的查询)可能更有效.

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

@model YourProject.ViewModels.ExampleViewModel

@foreach (var item in Model.CollectionA) { ... }
@foreach (var item in Model.CollectionB) { ... }
@foreach (var item in Model.CollectionC) { ... }
Run Code Online (Sandbox Code Playgroud)

对于更复杂的场景

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

让我们看看你的例子,看看它是如何工作的.所以,你现在正在寻找存储ts,lsd性能,让我们做一个类来存储那些在:

public class Example
{
     public string Ts { get; set; }
     public string Ls { get; set; }
     public string D { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

或者,如果您需要为不同的表构建不同的模型,然后将所有这些集合组合成类似于原始示例的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))
};
Run Code Online (Sandbox Code Playgroud)

其他形式的存储

根据您的需求,您可以考虑其他一些方法,例如使用该ViewBag集合.ViewBag是一个动态集合,允许您轻松存储和访问视图中的对象:

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();
Run Code Online (Sandbox Code Playgroud)

这基本上会以相同的方式工作,但@Model您只需使用视图中的引用而不是引用@ViewBag.值得注意的是,这也可以与实际模型一起使用.

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