我们如何在_layout中包含表单? [英] How can we include a form in _layout?

查看:83
本文介绍了我们如何在_layout中包含表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站的每个页面目前都使用_layout.cshtml.
我需要在显示为弹出窗口的每个页面上放置一个表单.
因此,我创建了一个新的PartialView(表单内容)及其对应的ViewModel,并在_layout.cshtml中对其进行了调用.

I currently have a _layout.cshtml used by every page of my website.
I need to put a form on each page displayed as a popin.
So, i created a new PartialView (the content of my form) with its corresponding ViewModel and called it in _layout.cshtml.

但是,我在使用布局的页面的ViewModels和新表单使用的ViewModel之间存在模型冲突(因为我们不能为同一视图直接拥有两个模型).

However, i have a model conflict between ViewModels of pages using the layout and the ViewModel used by the new form (since we can't have directly two models for the same view).

传递到字典中的模型项的类型为"XXX",但这是字典需要类型为'YYY'的模型项.

The model item passed into the dictionary is of type 'XXX', but this dictionary requires a model item of type 'YYY'.

在没有这种冲突的情况下,如何在_layout中包含表单?

How can we include a form in _layout without this conflict ?

推荐答案

以下内容对我有用,每个页面上都有一个侧边栏.

The following has worked for me with a sidebar on every page.

  1. 为您的局部视图创建控制器
  2. 在该控制器中,为您要返回的视图创建一个方法,并确保使用[ChildActionOnly]过滤器

  1. Create a controller for your partial view
  2. In that controller, create a method for the view you want to return, and be sure to use the [ChildActionOnly] filter

public class PartialController : Controller
{
    [ChildActionOnly]
    public PartialViewResult Alerts()
    {

        return PartialView("Alerts", messages);
    }
}

  • 在_layout视图中,您将具有以下内容:

  • In your _layout view, you'll have the following:

    @Html.Action("Alerts", "Partial")
    

    (而不是@ Html.RenderPartial或@ Html.Partial)

    (instead of @Html.RenderPartial or @Html.Partial)

    听起来您已经拥有所需的视图.

    It sounds like you already have what you need for the view.

    我还没有将其与表单一起使用,但是它应该可以类似地工作.希望这会有所帮助.

    I have not used this with a form, but it should work similarly. Hope this helps.

    这篇关于我们如何在_layout中包含表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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