【问题标题】:method is called twice方法被调用两次
【发布时间】:2021-11-04 21:09:24
【问题描述】:

我做错了什么? 我的方法被调用了两次。 我第一次得到 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();

}

脚本 我在我的视图上使用脚本使用帮助器@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 我可以返回 Json on My View 吗?

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

        return Json(ChoachList,JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

  • 如果您想使用您的 ajax 请求重定向到同一个视图页面,您可以使用下面的语句来完成,就像在您的 ajax 中一样,您正在调用带有参数的 ListCoach 方法,并且成功调用了相同的没有参数的方法导致 id 参数为空。 var geturl='/TrainingType/ListCoach?id='+ 数据; window.location.href=geturl;

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


【解决方案1】:

您的代码似乎没问题,理论上,您的操作不应调用两次,但我想向您展示一些方法来解决问题:

  1. 当在标签网络中的 chrome 开发者工具中调用服务器时,您会看到一条记录,其中有一个名为 Initiator 的列,显示调用是由谁和在哪里创建的。
  2. 您可以在函数 PassToContoller 的第一个函数中使用关键字 DEBUG 并查看堆栈以了解谁调用了您的函数

关于你的最后一个问题,我的回答是否定的,你不能使用这种方式将 JSON 对象传递给你的视图,但你可以通过 ViewBag 或 ViewData 在模型旁边传递 JSON 对象

【讨论】:

  • 可能会有帮助。第二次调用的时候我看take it out
  • 第二次调用我获取数据this这是我的Loyout页面
【解决方案2】:

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

如果你想在ajax中获取jsondata,你需要在ajax中使用dataType: "json",。 下面是一个传递json数据的demo:

行动:

[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 传递给操作(不使用 &lt;a&gt;&lt;/a&gt;):

<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" } });
        }

【讨论】:

  • 我想要Id,然后在我的方法中使用它并返回视图。 JsonResult 帮不了我
  • 您的意思是,您要返回 View("ViewName",ChoachList)。然后刷新视图?
  • 是的,这就是我想要得到的。
  • 您可以添加一个带有按钮的表单来替换,当点击按钮时,它会发布id到action。
  • 也许它能够解决我的问题:@Html.ActionLink("link caption", "ListCoach", "TrainingType", new { id = @item.Id }, new { @class = "btn btn-primary" })
猜你喜欢
  • 2013-12-31
  • 1970-01-01
  • 2020-07-16
  • 2014-09-02
  • 1970-01-01
  • 2014-05-22
  • 2017-02-15
  • 2016-10-03
  • 1970-01-01
相关资源
最近更新 更多