MVC 4 - 通过ViewBag数据传递到_layout局部视图 [英] MVC 4 - pass data via ViewBag to _Layout partial view

查看:1056
本文介绍了MVC 4 - 通过ViewBag数据传递到_layout局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个_Layout.cshtml文件,为每个主视图头部的局部视图。

I have a _Layout.cshtml files as a partial view as header on each main view.

我想定义的一个_layout选择元素和一些数据传递到使用ViewBag使数据填充的视图,可以在以后提交的局部视图。

I would like to define a Select element on the _Layout and pass some data to the partial view using ViewBag so that the data is populated on the view and can later be submitted.

我的问题是:


  • 哪里是定义包含的ActionResult功能和ViewBag定义数据?

  • 我该怎么做,如果我想提交的局部视图形式?凡和行动应定义/用于接受HttpPost命令?

谢谢!

推荐答案

我的建议是做一个基本的控制器类。
继承了它所有的控制器。
在code来呈现数据的布局,可以躺在里面的构造,或者说所有的控制器可以为这个基类的子类使用其他常用功能。

What I suggest is to make a base controller class. Inherit all your controllers from it. The code to render the data for the layout, can lie in it's constructor, or some other common function that all your controllers can use as children of this base class.

public class BaseController : System.Web.Mvc.Controller 
{
   public BaseController()
   {
     // This code will run for all your controllers
     ViewBag.MyData = "SomeData";
   }
}


关于你的问题:

如果我希望提交的局部视图形式我该怎么办?在哪里,哪些应该定义的动作/用于接受HttpPost命令?

What do I do if I want to submit a form on the partial view? Where and which action should be defined/used to accept the HttpPost command?

您可以只是把控制器名称的形式:

You can just put the controller name on your form:

@using (Html.BeginForm("ActionName", "Controller")) 

这篇关于MVC 4 - 通过ViewBag数据传递到_layout局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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