【问题标题】:transfer data from view to controller将数据从视图传输到控制器
【发布时间】:2021-07-15 03:01:48
【问题描述】:

如何做到这一点。 我要传输数据 我做了什么:

js

    $("#add-ling-user-couch").click(function () {
    var CoucnId = $(this).data('couch-id');
    var data = { "CoucnId": curentUserId };
    $.ajax({
        url: '/AppUser/AddLink',
        type: "POST",
        data: data,
        success: function (result) { console.log(result) }
       // dataType: dataType
    });
});

cshtml

 @foreach (var a in Model)
    {
        <tr>

            <td>@a.FirstName</td>

            <td>
                <div class="btn-group" role="group">
                    @Html.ActionLink("edit", "Edit", new { id = a.Id }, new { @class = "btn btn-secondary" })
                    @Html.ActionLink("Del", "Delete", new { id = a.Id }, new { @class = "btn btn-secondary" })
                    <a id="add-ling-user-couch" data-couch-id="@a.Id">Add Link</a>
                </div>

            </td>

        </tr>

控制器

        [HttpPost]
    [AllowAnonymous]
    public JsonResult AddLink(AddLinkViewModel model)
    {


        return new JsonResult()
        {
            Data = new { success = true, message ="Success" }    
        };
    }

我在使用它时遇到了一些问题。

其他方式怎么做呢? 我的方式带来的问题比解决的问题多

它会起作用,但我认为它不正确

<form method="post">
                                <input type="hidden" value="@a.Id" name="CoucnId" />
                                <input type="submit" value="Sender" />
                            </form>

【问题讨论】:

  • 你能改写一下“我希望用户可以关注沙发” - 不清楚这是什么意思。
  • 你需要阅读How to Ask,这个问题有点混乱,没有明确的问题陈述或领域,只有2块相对无关的代码
  • 表示:每个其他用户都可以在网站上订阅coach。如果用户已订阅,他将收到教练的通知。我改变了我的要求
  • 仍然不知道错误是什么?在服务器上发布错误(比如在客户端(Chrome 中的开发者工具是你的朋友),在服务器上......)没有这个,这个问题缺乏提供除 42 以外的答案的上下文(一个笑话,指 The Hitchhiker's Guide to银河)
  • 是的,这是真的。我之前错了。我的目标按钮就像一个 youtube 订阅。我希望有这样的选择。

标签: c# asp.net-mvc-4 model-view-controller asp.net-identity


【解决方案1】:

javascript:

$.post("/AppUser/AddLink", { CoucnId: curentUserId }, function (data) {     
   
    });

然后在控制器中返回带有模型的部分视图:

 [HttpPost]
    [AllowAnonymous]
    public JsonResult AddLink(int CoucnId)
    {
      fetch data and fill modle:
      return partialview("viewname",model);
    }

【讨论】:

  • 虽然此代码 sn-p 可能会解决问题,但including an explanation 将帮助人们了解您的代码建议的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多