简体   繁体   English

ASP视图中的多个模型

[英]Multiple models in ASP view

I have an ASP/MVC view that displays data from a model and also contains a partial view with a form that uses a different model. 我有一个ASP / MVC视图,该视图显示来自模型的数据,并且还包含带有使用不同模型的表单的局部视图。 Is there a way for me to combine these into one view? 我有办法将这些组合成一个视图吗? The result would be that the display values are based on model A but the form in the page submits model B. Is that possible? 结果将是显示值基于模型A,但是页面中的表单提交了模型B。这可能吗?

If the two things are closely related, you can use a view model to work with both in your view: 如果两者紧密相关,则可以在视图中使用视图模型进行操作:

public class FooViewModel
{
    public SomeModelForDisplay Foo { get; set; }
    public SomeModelForForm Bar { get; set; }
}

In your action, initialize both: 在您的操作中,初始化两个:

public ActionResult Foo(int id)
{
    var foo = db.Foos.Find(id);
    if (foo == null)
    {
        return new HttpNotFoundResult();
    }

    var model = new FooViewModel
    {
        Foo = foo,
        Bar = new SomeModelForForm()
    };
    return View(model);
}

If the two things are not related at all, or in particular, if the partial is being called in something like your layout instead of the immediate view, then it's more appropriate to use a child action. 如果这两件事根本不相关,或者特别是,如果部分布局是在您的布局(而不是即时视图)中调用的,那么使用子动作更为合适。 Essentially, you'll just handle the display part as if nothing else was going on: 本质上,您将像处理其他任何事情一样处理显示部分:

public ActionResult Foo(int id)
{
    var foo = db.Foos.Find(id);
    if (foo == null)
    {
        return new HttpNotFoundResult();
    }

    return View(foo);
}

Then, you'll add another action to handle the form: 然后,您将添加另一个操作来处​​理表单:

[ChildActionOnly]
public ActionResult SomeForm()
{
    var model = new SomeModelForForm();
    return PartialView("_SomeForm", model);
}

Then, add a partial view to render just the form: 然后,添加部分视图以仅呈现表单:

Views\\Foo\\_SomeForm.cshtml 查看\\富\\ _SomeForm.cshtml

@model SomeModelForForm

<!-- form fields here -->

Then, in your view/layout -- essentially wherever you want the form to actually be displayed: 然后,在您的视图/布局中-基本上是您希望实际显示表单的任何地方:

@Html.Action("SomeForm", "Foo")

Where "Foo" here is the name of the controller this child action is in. 其中"Foo"是此子操作所在的控制器的名称。

I recommand you to use model A that contains model B and C. B is loaded on the loading of the page and C is loaded with partial. 我建议您使用包含模型B和C的模型A。B加载在页面的加载中,C加载了局部的。

One other solution coule bethat you have a model A (given to razor page), then add model B to the page by using partial and then it return a mix of A and B. You just to make attention to the naming of the fields in order to make the model binding work properly, for instance 另一种解决方案是,如果您有一个模型A(给剃须刀页面),然后通过使用局部模型将模型B添加到页面中,然后返回模型A和B的混合。您只需要注意其中字段的命名即可为了使模型绑定正常工作,例如

A has

  • firstName 名字
  • lastName

and B has B有

  • street
  • phone 电话

Then, if you put edirotfor both fields, model that can be received in controller could be model C that has 然后,如果将edirot都放在两个字段中,则可以在控制器中接收的模型可能是具有以下条件的模型C

  • firstName 名字
  • lastName
  • street
  • phone 电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM