mvc问题,部分视图调度

时间:2015-10-14 14:05:49

标签: asp.net-mvc asp.net-mvc-4

我对部分视图有疑问。我想在我创建的标题登录表单中显示。在HomeController中,我有两个动作:一个是登录,另一个是使用httppost方法登录。 在局部视图(_Layout.cshtml)中我有一个代码 - 只发送页脚div:

 <td style="text-align:center">
        <h3>Bookstore</h3>
        @Html.Partial("_LoginPartial") 
 </td>

在登录视图中,我有:

@using(Html.BeginForm("Login", "Home", FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
    <tr>
        <td>@Html.LabelFor(a=>a.Username)</td>
        <td>@Html.TextBoxFor(a=>a.Username)</td>
        <td>@Html.ValidationMessageFor(a=>a.Username)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(a=>a.Password)</td>
        <td>@Html.PasswordFor(a=>a.Password)</td>
        <td>@Html.ValidationMessageFor(a=>a.Password)</td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>}

控制器

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

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Users u)
{
    if (ModelState.IsValid) {
        using (DatabaseLoginEntities dl = new DatabaseLoginEntities()) {
            var v = dl.Users.Where(a => a.Username.Equals(u.Username) && a.Password.Equals(u.Password)).FirstOrDefault();
            if (v != null) {
                Session["LogedUserId"] = v.UserAccountID.ToString();
                Session["LogedUserFullName"] = v.FullName.ToString();
                return RedirectToAction("AfterLogin");
            }
        }
    }
    return PartialView("_LoginPartial", u);
}

当按下登录按钮时,mvc将我切换到显示登录表单的单独页面。我不希望这种情况发生。它必须留在头部。功能还可以,但我已经发现如何仅在标题中显示。有没有人知道我哪里错了?

1 个答案:

答案 0 :(得分:0)

你必须使用ajax

 @using (Ajax.BeginForm("Login", "Home",
                                      new AjaxOptions
                                      {
                                          HttpMethod = "Post",
                                          InsertionMode = InsertionMode.Replace
                                          ,
                                          UpdateTargetId = "div_id"
                                      }
                                    )){
<table>
    <tr>
        <td>@Html.LabelFor(a=>a.Username)</td>
        <td>@Html.TextBoxFor(a=>a.Username)</td>
        <td>@Html.ValidationMessageFor(a=>a.Username)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(a=>a.Password)</td>
        <td>@Html.PasswordFor(a=>a.Password)</td>
        <td>@Html.ValidationMessageFor(a=>a.Password)</td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>
}