简体   繁体   English

母版页中的MVC3部分视图

[英]MVC3 Partial view inside Master Page

On my site, I want to display a Login box on all pages. 在我的网站上,我想在所有页面上显示一个“登录”框。 So I wanted to make a partial view page, which I use on my _Layout.cshtml file in Shared. 因此,我想制作一个局部视图页面,该页面将用于共享的_Layout.cshtml文件中。

But where would the controller for this partial view go? 但是这个局部视图的控制器会去哪儿呢? And how would my Login button have access to it? 我的“登录”按钮将如何访问它?

So, when the Request.IsAuthenticated is true, the login box shows 'Logged in as ...', but when the result is false, I get a little table with the usual Username/Password form. 因此,当Request.IsAuthenticated为true时,登录框将显示“ Logging as ...”,但结果为false时,我会得到一个带有通常的Username / Password形式的小表。

Edit: After trying some answers below, I seem to be stuck in an endless lopp on the GET method below. 编辑:在尝试了以下一些答案之后,我似乎陷入了下面的GET方法的无休止的困境。 It it because my partial view is trying to load me _Layout.cshtml file, as it want to accosiate the 'masterpage' with the partial view? 这是因为我的局部视图正试图加载_Layout.cshtml文件,因为它想让“母版页”与局部视图相对应? And because my partial view is being rendered in _Layout.cshtml, it's lopping? 而且由于我的局部视图是在_Layout.cshtml中呈现的,因此变得不完整了吗?

public class LoginController : Controller
{
    //
    // GET: /Login/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(LoginModel loginModel)
    {
        if(ModelState.IsValid)
        {
            var g = new GallaryImage();
            var user = g.LoginUser(loginModel.Username, loginModel.Password);
            if(user != null) 
            {
                FormsAuthentication.SetAuthCookie(user.username, false);
                return RedirectToAction("index", "Home");
            }
            ModelState.AddModelError("", "Invalid Username/Password");
        }
        return View();
    }

In my _Layout.cshtml, I am trying to load the partial view like this: 在我的_Layout.cshtml中,我试图像这样加载部分视图:

<div style="text-align: right">
      @Html.Action("Index", "Login")
</div>

See the issue? 看到问题了吗?

You can give controller name as attribute to Html.Action method 您可以将控制器名称作为属性赋予Html.Action方法

Html.Action("ActionName", "ControllerName")

If you use Razor syntax, you must write @Html.Action("ActionName", "ControllerName") in your view and your Action with name ActionName should return PartialView(which wil be your login area). 如果使用Razor语法,则必须在视图中编写@Html.Action("ActionName", "ControllerName") ,并且名称为ActionName的Action应该返回PartialView(将是您的登录区域)。

FYI, Views don't have controllers. 仅供参考,视图没有控制器。 Controllers have Views. 控制器具有视图。 The distinction may seem subtle, but it's not. 这种区别似乎微妙,但事实并非如此。 A view can be used by any number of controllers, and views don't care or know about the controllers. 一个视图可以被任意数量的控制器使用,而视图并不关心或不知道控制器。 So you have to think about the current URL, which means the current Action Method. 因此,您必须考虑当前的URL,即当前的操作方法。

In the case of your login partial, it doesn't need much if anything from the Controller. 如果您是部分登录,则从Controller那里不需要太多。 It's directly accessing the User property of the page to find out if it's authenticated. 它直接访问页面的User属性以查找其是否已通过身份验证。 Your login button is just a form with it's action method set to your login method of your account controller. 您的登录按钮只是一种表单,其操作方法设置为帐户控制器的登录方法。

Even the username can be displayed from the User property of the page. 甚至用户名也可以从页面的“用户”属性中显示。

Just look at the default MVC app that is generated when you create a new Internet project. 只需查看创建新的Internet项目时生成的默认MVC应用。 It has all this functionality already implemented. 它已经实现了所有这些功能。 Just copy it. 只需复制即可。

Try to use HTML.RenderAction("Action", "Controller") and make sure you are not calling your master page from within the partial view. 尝试使用HTML.RenderAction("Action", "Controller")并确保您没有从局部视图中调用母版页。 This has the potential to produce a loop. 这有可能产生回路。

I'm using MVC 3 ASPX view engine not razor though. 我正在使用MVC 3 ASPX视图引擎,而不是剃刀。

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

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