asp.net-mvc ASP.NET MVC - 使用多个模型查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/944334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:38:00  来源:igfitidea点击:

ASP.NET MVC - View with multiple models

asp.net-mvcview

提问by alex2k8

I am trying to generate such HTML

我正在尝试生成这样的 HTML

    <form action="/some/process" method="post">
        <input type="hidden" name="foo.a" value="aaa"/>
        <input type="hidden" name="bar.b" value="bbb"/>
        <input type="submit" />
    </form>

so it can be processed by this Action:

所以它可以被这个Action处理:

    public ActionResult Process(Foo foo, Bar bar)
    {
        ...
    }

Given the Action code

鉴于动作代码

    public ActionResult Edit()
    {
        ViewData["foo"] = new Foo { A = "aaa" };
        ViewData["bar"] = new Bar { B = "bbb" };

        return View();
    }

what should I write in Edit.aspx view? I don't want to write names 'foo.a' and 'bar.b' manually.

我应该在 Edit.aspx 视图中写什么?我不想手动编写名称 'foo.a' 和 'bar.b'。

回答by Wyatt Barnett

String-indexed ViewDatais bad. What you probably want to do is make a little wrapper class for your multi-variable view data and pass that to a strongly typed view. IE:

字符串索引ViewData不好。您可能想要做的是为您的多变量视图数据创建一个小包装类,并将其传递给强类型视图。IE:

public class FooBarViewData
{
   public Foo Foo {get; set;}
   public Bar Bar {get; set;}
}
public ActionResult Edit()
{
   FooBarViewData fbvd = new FooBarViewData();
   fbvd.Foo = new Foo(){ A = "aaa"};
   fbvd.Bar = new Bar(){ B = "bbb"};
   return View(fbvd);
}

Then your view is just strongly typed to FooBarViewDataand you can call members of that object using the Modelproperty.

然后您的视图只是强类型化FooBarViewData,您可以使用该Model属性调用该对象的成员。

回答by tvanfosson

You have a couple of choices. First, you can reference them from ViewData and use an HtmlHelper extension. Or you could create a view-specific model and use a strongly-typed viewpage for Edit.aspx.

你有几个选择。首先,您可以从 ViewData 引用它们并使用 HtmlHelper 扩展。或者,您可以创建特定于视图的模型并为 Edit.aspx 使用强类型视图页面。

public class EditModel
{
    public Foo foo { get; set; }
    public Bar bar { get; set; }
}

public ActionResult Edit()
{
    var model = new EditModel();

    model.foo = new Foo { A = "aaa" };
    model.bar = new Bar { B = "bbb" };

    return View( model );
}

(Edit.aspx is of type ViewPage<EditModel>)

(Edit.aspx 是类型ViewPage<EditModel>

Either way, the HtmlHelper extension will pick up any initial values.

无论哪种方式,HtmlHelper 扩展都会选择任何初始值。

<form action="/some/process" method="post">
     <%= Html.Hidden( "foo.A" ) %>
     <%= Html.Hidden( "bar.B" ) %>
</form>

回答by David Lee

The solution above may be outdated. This solution seems to work for ASP.Net MVC5+.

上面的解决方案可能已经过时。此解决方案似乎适用于 ASP.Net MVC5+。

You're going to have to use the ViewModelmethod. This is an excellent tutorial that you can check out.

您将不得不使用该ViewModel方法。这是一个很好的教程,你可以看看。

http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example

http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example

You are going to have to join multiple models into one ViewModeland grab all the properties from each models you want to use into the ViewModel.

您将不得不将多个模型合并为一个模型,ViewModel并从您要使用的每个模型中获取所有属性到ViewModel.

BUT, it is HIGHLYadvised that you create a new Controllerand a new Viewto accomodate the newly created ViewModel. Read the tutorial.

但是强烈建议您创建一个新的Controller和一个新的View来容纳新创建的ViewModel. 阅读教程。

public class FooBarViewModel
{
   public string A {get; set;}  //Property for Foo
   public string B {get; set;}  //Property for Bar
}

public ActionResult Edit()
{
   FooBarViewModel fooBarVM = new FooBarViewModel();
   fooBarVM.A = "aaa";
   fooBarVM.B = "bbb";

   return View(fooBarVM);
}

But in this case, you should be able to pass the ViewModelinto a different view. Just make sure you declare this similar directive correctly in the foobar.cshtmlpage.

但是在这种情况下,您应该能够将 传递ViewModel到不同的视图中。只要确保在foobar.cshtml页面中正确声明了这个类似的指令。

@model FooBar.Models.FooBarViewModel

回答by Abdul G

I was struggling for a while trying to get two models to work, and most of the answers I came across was meant for earlier versions of MVC.

我挣扎了一段时间试图让两个模型工作,我遇到的大多数答案都是针对早期版本的 MVC。

I found the following tutorial works best for MVC 5 as suggested by David Lee:

我发现以下教程最适合David Lee建议的 MVC 5 :

http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example

http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example

I created a join and selected only the columns I needed from both Models.

我创建了一个连接并仅从两个模型中选择了我需要的列。

I had it working with partial views

我让它与部分视图一起工作

In view.cshtml

在view.cshtml

 @model IEnumerable<Test.Models.TestViewModel>
    ...
      @foreach (var item in Model)
                    {
                        <tr>
                            <td style="text-align:center;vertical-align:middle">
                                @Html.DisplayFor(modelItem => item.TestName)
...