【问题标题】:Passing data to controller in ASP.NET MVC using C#使用 C# 将数据传递给 ASP.NET MVC 中的控制器
【发布时间】:2021-09-07 01:03:03
【问题描述】:

我做错了什么?

我想将Id 传递给我可以使用它的控制器。

我的控制器

[HttpGet]
public ActionResult ListCoach(int id)
{
    List<Training> ListCoachTraining = _TraningService.ListCoach(id);
    var ChoachList = _TraningService.Coach(id);
    return View(ChoachList);
}

我的观点 - 调用脚本的正确方法是什么?它现在不起作用:

 <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/no_foto.png" 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 href="~/TrainingType/ListCoach" id="btnSave" class="btn btn-primary">Go to anywhere</a>
                                <a id="btnSave" class="btn btn-primary">Go to anywhere</a>
                                <input class="btn btn-primary" type="button" id="btnSave" value="Save" />
                            </div> 
                        </div>
                    </div>
                </div>
                }
        </div>
    </div>
//call the script
<script src="@Url.Content("~/scripts/ManagerAccount.js")" type="text/javascript"></script>

我的脚本 - 我使用 Ajax。那里我想将id 传递给控制器​​:

function PassToContoller(data) {
    $("#btnSave").click(function () {

        $.ajax({
            type: "Get",
            url: '/TrainingType/ListCoach',
            data: { id: data },
            success: function (data) {
                window.location.href = '/appUser/ManageAccount';
                //return data;
            },
            error: function () {
                $("#loader").fadeOut("slow");
                console.log("error1");
            }
        });
    });
};

【问题讨论】:

  • 如何路由到 ListCoach 方法?你定义了正确的路由吗? $.ajax(: type:"GET", (get 是大写不知道有没有区别)
  • 您能否分享您的Training 班级定义。以便它可以相应地再现。
  • 是的,我编辑 rout 是错误的,但它不能正常工作 route: url: '/TrainingType/ListCoach',
  • 请尝试Update with Comment Can I do without? 部分的解决方案,其中包含您上一条评论的解决方案。现在已经展示了每一种方式,您可以选择实施哪种方式。

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

您还没有相应地调用function。您希望您的 PassToContoller 函数应该在您单击 save 时调用,在您的情况下,您必须使用 btnSave onlickonClick="PassToContoller(@item.Name)" 调用此函数

我在这里向你们展示:

onClick="PassToContoller(@item.Name)" 排队时:

Razor HTML

<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/no_foto.png" 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 href="~/TrainingType/ListCoach" id="btnSave" class="btn btn-primary">Go to anywhere</a>
                            <a id="btnSave" class="btn btn-primary">Go to anywhere</a>
                            <input class="btn btn-primary" type="button" id="btnSave" onClick="PassToContoller(@item.Name)" value="Save"  />
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

脚本:

<script>
    function PassToContoller(data) {
        alert(data);
        $.ajax({
            type: "GET",
            url: '/ListCoachController/ListCoach',
            data: { id: data },
            success: function (data) {
                console.log(data);
                window.location.href = '/appUser/ManageAccount';
                return data;
            },
            error: function () {
                $("#loader").fadeOut("slow");
                console.log("error1");
            }
        });
    }
</script>

输出:

另一种方式 btnSave Onlick:

在这方面你需要做一些修改:

需要引入一个输入类型的文本,它会被隐藏 约束像 &lt;input type="text" id="getValue" value="@item.Name" hidden /&gt; 这样的值,并在你传递隐藏值时 会点击按钮。

另一件事是您的btnSave 似乎您对其他按钮也使用了相同的按钮,因此您的onclick 函数将混淆要触发的事件。所以需要设置unique btn id 现在剩下的和下面一样:

Razor HTML

<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/no_foto.png" 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 href="~/TrainingType/ListCoach" id="btnSave" class="btn btn-primary">Go to anywhere</a>
                            <a id="btnSave" class="btn btn-primary">Go to anywhere</a>
                            <input class="btn btn-primary" type="button" id="btnNewSave" value="Save" />
                            <input type="text" id="getValue" value="@item.Name" hidden />
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

脚本:

@section scripts {
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btnNewSave").click(function () {
                var data = $("#getValue").val();
                $.ajax({
                    type: "GET",
                    url: '/ListCoachController/AnotherListCoach',
                    data: { id: data },
                    success: function (data) {
                        console.log(data);
                        window.location.href = '/appUser/ManageAccount';
                        return data;
                    },
                    error: function () {
                        $("#loader").fadeOut("slow");
                        console.log("error1");
                    }
                });
            });
        });
    </script>
}

更新: 根据您对评论的新要求,您只需更新您的HTML,如下所示:

<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/no_foto.png" 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 href="~/TrainingType/ListCoach"  class="btn btn-primary" onClick="PassToContoller(@item.Name)">Go to anywhere</a>
                            <a  class="btn btn-primary" onClick="PassToContoller(@item.Name)">Go to anywhere</a>
                            <input class="btn btn-primary" type="button" onClick="PassToContoller(@item.Name)" value="Save" />
                            <input type="text" id="getValue" value="@item.Name" hidden />
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

注意:您必须从所有&lt;a&gt;&lt;button&gt; 中去掉id="btnSave" 并需要替换为 onClick="PassToContoller(@item.Name)" 它会通过你所有的 价值。不管你有多少个周期。

更新评论 Can I do without?

HTML:

<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/no_foto.png" 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 href="#"  class="btn btn-primary" >Go to anywhere</a>
                            <a  class="btn btn-primary">Go to anywhere</a>
                            <input class="btn btn-primary" type="button"  value="Save" />
                            <input type="text" id="getValue" value="@item.Name" hidden />
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

脚本:

 @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: '/UserLog/AnotherListCoach',
                    data: { id: data },
                    success: function (data) {
                        console.log(data);
                        window.location.href = '/appUser/ManageAccount';
                        return data;
                    },
                    error: function () {
                        $("#loader").fadeOut("slow");
                        console.log("error1");
                    }
                });
            }
            $(document).ready(function () {
               
                $("a").click(function () {
                    var data = $("#getValue").val();
                    var callFunc = PassToContoller(data)
                });
                $(":button").click(function () {
                    var data = $("#getValue").val();
                    var callFunc = PassToContoller(data)
                });
                
            });
        </script>
    }

希望它能相应地指导您。您可以使用任何一种方法。

【讨论】:

  • 谢谢先生,它成功了。但我不明白。我有循环对于我得到六个或十个值的地方` ` 如果我选择第二个或第三张牌
  • 您采用哪种方法?我看到你只有@item.Name?
  • 你的意思是点击哪个按钮你想调用同一个函数吗?
  • 是的,我想这样做。有可能吗?
  • 是的,可能需要动态设置按钮 ID,这是不同的问题,但这里你的主要问题是如何调用我主要关注的函数。
【解决方案2】:

使用GET 方法,您应该通过查询字符串传递数据 /TrainingType/ListCoach?id=1

在 ASP.NET MVC 中,我们的默认路由模板是 {Controller}/{Action}/{id} 所以,我们可以使用这个 URL /TrainingType/ListCoach/1

【讨论】:

  • 如果我将 GEt 更改为 POST。我可以使用 Jquery 将数据传递给控制器​​吗?
  • 你可以使用其中任何一个,我给你指路。
  • @Boris:你能检查一下data 的值吗?如果它包含非数字字符,则id 无法绑定,因为它的数据类型是int
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多