从LoginPartialView重定向到另一个控制器中的操作

时间:2017-07-31 15:03:50

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

我已经定义了以下部分视图_LoginPartialView.cshtml

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">

        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown"> @User.Identity.GetUserName()<span class="caret"></span></a>
            <ul class="dropdown-menu" role="menu">
                <li>@Html.ActionLink("Zarządzaj kontem", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })</li>
                <li>@Html.ActionLink("Edytuj dane osobowe", "Edit", "Users", new { id=User.Identity.GetUserId()})</li>
                <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
            </ul>
        </li>
    </ul>
    }
}

当我点击&#34; Edytuj dane osobowe&#34;时,它会将我重定向到默认路线

http://localhost:xxxxx/Home/Edit?Length=5

我使用方法Edit定义了UsersController,它看起来像这样

public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = repository.GetUserById(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

当我手动输入带参数id的网址时,它会显示正确的页面。这种奇怪的重定向是什么原因?

1 个答案:

答案 0 :(得分:0)

请尝试替换

<li>@Html.ActionLink("Edytuj dane osobowe", "Edit", "Users", new { id=User.Identity.GetUserId()})</li>

with:

<li>@Html.ActionLink("Edytuj dane osobowe", "Edit", "Users", new { id=User.Identity.GetUserId()}, null)</li>