部分视图不会调用Http Post方法

时间:2018-11-13 11:28:24

标签: c# razor model-view-controller partial-views

因此,我在主页中创建了部分登录。表单可以正确呈现,但是每当我提交表单时,它都不会调用http post操作结果。请参阅以下代码以供参考。

首页:

<header class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarTogglerDemo01">


        <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            <li class="nav-item active">
                <a href="@Url.Action("HomePage", "Home")" class="nav-link">HOME</a>
            </li>
            <li class="nav-item">
                <a href="@Url.Action("Branchlist", "Branch")" class="nav-link">BRANCH</a>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <div> @{Html.RenderAction("LogInBox");}</div>>
        </form>
    </div>
</header>

部分登录

@model BBLoyalty.Models.LoyaltyAccountModel

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm("LogInBox", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div class="form-row align-items-center">

<div class="col-auto">
    <label class="sr-only" for="inlineFormInputGroup">Username</label>
    <div class="input-group mb-2">
        <div class="input-group-prepend">
            <div class="input-group-text"><i class="fas fa-user"></i> </div>
        </div>
        @Html.TextBoxFor(s => s.Username, new { @class = "form-control", placeholder = "Username", autofocus = "autofocus" })
    </div>
</div>

<div class="col-auto">
    <label class="sr-only" for="inlineFormInputGroup">Username</label>
    <div class="input-group mb-2">
        <div class="input-group-prepend">
            <div class="input-group-text"><i class="fas fa-lock"></i> </div>
        </div>
        @Html.PasswordFor(s => s.Password, new { @class = "form-control", placeholder = "Password" })
    </div>
</div>

<div class="col-auto">
    <button type="submit" class="btn btn-secondary mb-2">Log In</button>
</div>
</div>

}

HomeController.cs

    public ActionResult LogInBox()

    {
        return View();
    }


    [HttpPost]
    public ActionResult LogInBox(LoyaltyAccountModel acn)
    {
        if (ModelState.IsValid)
        {
            LoyaltyAccount ua = bbdb.LoyaltyAccounts.FirstOrDefault(x => x.Username == acn.Username && x.IsActive == true);
            if (ua != null)
            {
                string ep = eh.GetEncrypted(acn.Password, acn.Username);

                if (ua.Password == ep && ua.Type != "") // Needs to Identify the type of Account(Client/Admin) Otherwise Cannot Login
                {

                    Session["ActiveUser"] = ua.UserId;

                    if (ua.Type == "Admin" || ua.Type == "SuperUser")
                    {
                        return RedirectToAction("Index", "Maintenance");
                    }
                    else
                    {
                        return RedirectToAction("Index", "Profile");
                    }
                }
                else
                {
                    ViewBag.Message = "Incorrect Password!";

                    return View();
                }
            }
            else
            {
                ViewBag.Message = "Invalid Account!";

                return View();
            }
        }
        else
        {
            return View();
        }
    }

提交后,它不会调用ActionResult LogInBox(LoyaltyAccountModel acn)

请帮助。它总是调用第一个ActionResult,即没有参数的那个。

enter image description here

1 个答案:

答案 0 :(得分:0)

由于您在Home中有表单标签,而在Partial中有另一个表单标签,因此这会在运行时生成嵌套表单,这是不受支持的。

从下面的代码中删除表单标签。

<form class="form-inline my-2 my-lg-0">
            <div> @{Html.RenderAction("LogInBox");}</div>>
</form>