ASP.NET返回多个变量以进行查看 [英] ASP.NET Returning Multiple Variables to View

查看:83
本文介绍了ASP.NET返回多个变量以进行查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何将多个变量返回到视图时遇到麻烦。这样的东西。我可以得到一点帮助吗?

I am having trouble figuring out how to return multiple variables to a view. Something like this. Can I get a little help?

    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:

Consider Using a ViewModel

You'll want to use a ViewModel to compose all of these different results and pass that model to the 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; }
}

然后使用类似下面的代码来执行您的特定查询,使用这些查询的结果来构建您的模型,然后将其传递给视图:

And then just use something like the following code to perform your specific queries and use the results of those queries to build your model and then pass it along to the 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.

然后,在视图中,您可以按预期引用基础属性集合并对其进行迭代或执行任何其他类型的操作:

Then within the View, you can reference the underlying property collections as expected and iterate through them or perform any other types of operations:

@model YourProject.ViewModels.ExampleViewModel

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



用于更复杂的情况



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

For More Complex Scenarios

If you didn't want to simply access a single column from your database but rather multiple, you'll likely want to create another model/class to map your properties and then store instances of those within your ViewModel.

让我们看看您的示例,看看它可能如何工作。因此,您目前正在存储 ts ls d 属性,因此让我们创建一个类来存储这些属性:

Let's look at your example and see how that might work. So you currently are looking to store the ts, ls and d properties, so let's make a class to store those in:

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

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

Now, when you perform your query, simply grab all of these and map them within a Select() call:

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

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

You could now pass this along directly to your View if that's all that you needed:

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

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

Or you could perform multiple of these if you needed to build different models for different tables and then compose all of those collections into a ViewModel similar to the original example:

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中存储和访问对象:

Other Forms Of Storage

There are a few other approaches you could consider depending on your needs, such as using the ViewBag collection. The ViewBag is a dynamic collection that allows you to easily store and access objects within the 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();

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

This will essentially work the same way, but instead of references @Model within your View, you would just use @ViewBag instead. It's also worth noting that this can also be used in conjunction with an actual model as well.

还有其他方法,例如使用 ViewData Session 集合,但是您真的应该使用模型。它更符合实际的MVC模式,如果您习惯了它,它应该可以使您的生活更加轻松。

There are other approaches such as using the ViewData or Session collections, but you really should use a model. It's more in line with the actual MVC pattern, and if you get used to it, it should make your life much easier.

这篇关于ASP.NET返回多个变量以进行查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆