首页 > 解决方案 > 方法被调用两次

问题描述

我做错了什么?我的方法被调用了两次。我第一次得到 Id 并且这是正确的行为第二次得到 Null 我无法得到它什么是解决它的正确方法?

我的观点

 <div class="container">
    <div class="row">
        @foreach (var item in Model)
        {
            <div class="col-md-4">
                <div class="card" style="width: 18rem;">
                    <img src="~/Content/not-found-image.jpg" class="card-img-top" alt="...">
                    <div class="card-body">
                        <h5 class="card-title">@item.Name</h5>
                        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                        <div id="textButton">
                            <a class="btn btn-primary" data-index="@item.Id" name="link">Go to anywhere</a>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

我的控制器

[HttpGet]
public ActionResult ListCoach(int id)
{    
    if (id != null)
    {
        var ChoachList = _TraningService.Coach(id);
        return View(ChoachList);
    }

    return HttpNotFound();

}

脚本 我在 My View上使用脚本使用助手 @Section {}

@section scripts {
    @*<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>*@
    <script>
        function PassToContoller(data) {
            //alert(data);
            $.ajax({
                type: "GET",
                url: '/TrainingType/ListCoach',
                data: { id: data },
                success: function (data) {
                    console.log(data);
                    window.location.href = '/TrainingType/ListCoach';
                    return data;
                },
                error: function () {
                    $("#loader").fadeOut("slow");
                    console.log("error1");
                }
            });
        }
        $(document).ready(function () {
            $("a[name=link]").on("click", function () {
                var valueOfClickedTag = $(this).data("index");
                alert("Clicked: " + valueOfClickedTag);
                var callFunc = PassToContoller(valueOfClickedTag)
            });

            $(":button[id=GoToAnywhere3]").on("click", function () {
                var valueOfClickedBtn = $(this).data("index");
                var callFunc = PassToContoller(valueOfClickedBtn)
            });
        });
    </script>
}

如果我使用以下方法: 我无法进入 View ListCoach 我可以在 My View 上返回 Json 吗?

[HttpGet]
    public JsonResult ListCoach(int id)
    {
        var ChoachList = _TraningService.Coach(id);

        return Json(ChoachList,JsonRequestBehavior.AllowGet);
    }

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

解决方案


在你的函数PassToContoller中,在 ajax 中success,你使用window.location.href = '/TrainingType/ListCoach';,所以在你调用函数之后,它将有两个请求,一个使用 ajax,一个使用window.location.href.

如果要在ajax中获取jsondata,需要dataType: "json",在ajax中使用。这是一个传递 json 数据的演示:

行动:

[HttpGet]
        public JsonResult GetJson(int id)
        {
            return new JsonResult(new TestModel { Id=id,Name="Test"});
        }

看法:

<button onclick="test()">
    test
</button>
@section scripts
{
    <script>
        function test() {
           $.ajax({
                type: "GET",
                url: 'GetJson',
                data: { id: 1 },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    
                },
                error: function () {
                    $("#loader").fadeOut("slow");
                    console.log("error1");
                }
            });
        }
    </script>
}

结果: 在此处输入图像描述

更新:

您可以尝试使用表单将 Id 传递给操作(不使用<a></a>):

<form asp-action="GetJson" asp-route-id="@item.Id">
    <input type="submit" value="Go to anywhere" />
</form>

行动:

[HttpGet]
        public IActionResult GetJson(int id)
        {
            return View("Index",new List<TestModel> { new TestModel { Id = id, Name = "Test" } });
        }

推荐阅读