C#MVC将一个局部视图替换为另一个

时间:2017-08-30 15:00:19

标签: c# ajax asp.net-mvc partial-views

我正在使用MVC建立一个网站,我希望它在头部导航栏上有一个小框,用户可以登录到他的帐户。头部导航栏是与我拥有的所有视图共享的布局页面,并且在布局中呈现登录的部分视图。

这是部分视图放置的布局:

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            @Html.ActionLink("חנות השיגועים של האחים וויזלי", "Master", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <ul class="nav navbar-nav">
            <li>@Html.ActionLink("בית", "Master", "Home")</li>
            <li>@Html.ActionLink("אודות", "About", "Home")</li>
            <li>@Html.ActionLink("צור קשר", "Contact", "Home")</li>
            <li>@Html.ActionLink("סל קניות", "Cart", "Home")</li>
            <li>@Html.ActionLink("פרופיל", "UserProfile", "Home")</li>
            <li>@Html.ActionLink("הרשמה", "Register", "Account")</li>

        </ul>
        @*<ul class="nav navbar-nav navbar-right">
            <li>*@

                <div id="PartialPlace">
                    @Html.Partial("_LoginPartial")
                </div>
            @*</li>

        </ul>*@
    </div>
</nav>

<div class="container body-content">
    @RenderBody()

    <img src="~/Assets/Site_Background.jpg" alt="Weasly Shop" id="BkgImage" class="bg"/>

    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
</div>

这是登录部分代码:

<form method="post" id="LoginFloat" action="/account/login">

<div class="container">
    <label><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="UserName" required>

    <label><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="Password" required>

    <button type="submit" class="btn btn-primary">Login</button>

   @*@Ajax.ActionLink("Login", "Login", "Account", new AjaxOptions() { UpdateTargetId = "divToUpdate" }, new { @class = "btn btn-primary" })*@

    <input type="checkbox" checked="checked"> Remember me

</div>

这就是行动:

[HttpPost]
    public ActionResult Login(string UserName, string Password)
    {

        using (AspStoreDBEntities1 db = new AspStoreDBEntities1())
        {
            if (ModelState.IsValid)
            {
                foreach (var user in db.Users)
                {
                    if (user.UserName == UserName && user.Password == Password)
                    {
                        FormsAuthentication.SetAuthCookie(UserName, true);
                        return PartialView("_LogedInPartial");
                    }
                }

            }
            return View();
        }
    }

我想要做的是当用户在布局中按下登录部分中的登录按钮时,只有部分视图会刷新,并且会向用户显示他可以发布广告的部分视图:

<div class="container">
<div class="jumbotron" id="LoginFloat">
    <h5>Hello @Model.UserName</h5>

    <button class="btn btn-warning" onclick="">Advertise!</button>
    <br />
    <input type="submit" name="LogOff" value="LogOff" />LogOff
</div>

我尝试使用Ajax.Actionlink进行替换操作,但我无法弄清楚如何使用它。我应该在布局页面或Login partialView中使用它还是在哪里?

0 个答案:

没有答案