ActionLink控制器被调用两次

时间:2019-09-28 12:09:27

标签: c# asp.net asp.net-mvc entity-framework razor

我有一个带有角色列表的表格视图和一个用于创建新角色的操作链接。以下是我的role.cshtml页面

@model List<ExpenCare.Models.CompanyRole>

<div class="row">
@Html.Partial("_RightSidePane")
<div class="col-10 col-sm-8 col-xs-8 col-md-10">
    <div class="tab-content" id="nav-tabContent">
        <div class="tab-pane fade" id="list-Roles-Content" role="tabpanel" aria-labelledby="list-Roles"></div>
        @using (@Html.BeginForm("UpdateRoles", "Admin", FormMethod.Post))
        {
            <table class="table">
                <tr>
                    <th>
                        Name
                    </th>
                    <th>
                        Enable
                    </th>
                </tr>

                @for (var i = 0; i < Model.Count(); i++)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(m => m[i].Id)
                            @Html.HiddenFor(m => m[i].Name)
                            @Html.DisplayFor(m => m[i].Name)
                        </td>
                        <td>
                            @Html.EditorFor(m => m[i].Enabled, new { @checked = "checked" })
                        </td>
                    </tr>
                }

            </table>
            <p><input type="submit" value="Save Changes" /></p>
            <p style="color:green; font-size:12px;">@ViewBag.Message</p>
            <p>
                @Html.ActionLink("Create new Roles", "ViewCreateRoles", "Admin", null, new { id = "CreateRoles", @class = "appao-btn" })
            </p>
        }
    </div>
</div>
</div>

“创建新角色”按钮调用管理控制器中的ViewCreateRoles方法,如下所述。

public ActionResult ViewCreateRoles()
{
   return View("Roles/CreateRoles");
}

在CreateRoles.cshtml页面中,用户可以创建角色并提交。提交时,它会重定向到同一管理控制器中的操作CreateRoles。

@model ExpenCare.Models.CompanyRole

@{
    ViewBag.Title = "CreateRoles";
}

<div class="row">
    @Html.Partial("_RightSidePane")
    <div class="col-10 col-sm-8 col-xs-8 col-md-10">
        <div class="tab-content" id="nav-tabContent">
            <div class="tab-pane fade show active" id="list-Profile-Content" role="tabpanel" aria-labelledby="list-Profile">

                @using (Html.BeginForm("CreateRoles", "Admin", FormMethod.Post))
                {
                    @Html.AntiForgeryToken()

                    <div class="form-horizontal">
                        <hr />
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Enabled, "Enable", htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                <div class="checkbox">
                                    @Html.EditorFor(model => model.Enabled)
                                    @Html.ValidationMessageFor(model => model.Enabled, "", new { @class = "text-danger" })
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Create" class="btn btn-default" />
                            </div>
                        </div>
                    </div>
                }
                </div>
        </div>
        </div>
    </div>

下面是管理控制器中的CreateRoles方法。

public ActionResult CreateRoles(CompanyRole role)
        {
            List<CompanyRole> roles;
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:1888/api/");
            var responseTask = client.PostAsJsonAsync("CompanyRoles", role);
            responseTask.Wait();

            var result = responseTask.Result;
            if (result.IsSuccessStatusCode)
            {
                var readTask = result.Content.ReadAsAsync<List<CompanyRole>>();
                readTask.Wait();

                roles = readTask.Result;
                return View("Roles/Roles", model: roles);
            }
            else
            {
                ViewBag.Message = "not updated.";
                return View("Roles/CreateRoles");
            }

        }

成功创建角色后,用户将重定向到上面提到的role.cshtml,其中显示了所有角色。我的问题是我添加角色的过程(转到角色页面->创建新角色->重定向到角色页面)工作正常。但是在重定向到角色页面之后,我的Web API中出现“在ExpenCare.dll中发生类型'System.Web.Http.HttpResponseException'的异常,但未在用户代码中处理”的错误,该错误在CreateRoles中被调用。当我调试过程时,我看到在转到管理控制台的“ Dispose”方法后,将再次调用“ CreateRoles”方法,并将空的“ CompanyRole角色”数据传递到Web API。我不知道如何再次调用它。我检查了google,并尝试从视图和布局页面的href和src中删除所有“#”,但仍无法正常工作。我找不到为什么在没有单击按钮的情况下两次调用相同方法的原因。

0 个答案:

没有答案
相关问题