使用 asp.net mvc 5 动作控制器按名称读取 ajax 请求主体值

reading ajax request body values by name with asp.net mvc 5 action controller

我想访问 jquery ajax post 请求正文以获取我的 asp.net mvc 5 控制器操作的值。 知道我成功通过了 __RequestVerificationToken。

<script>
$(document).ready(
       function () {
           $("#renamedashboard").click(function () {
               swal({
                   title: "Titre Dashboard",
                   text: "Saisir le nouveau titre:",
                   type: "input",
                   showCancelButton: true,
                   closeOnConfirm: false,
                   animation: "slide-from-top",
                   inputType : "text"
               },
               function (inputValue)
               {
                   if (inputValue === false)
                       return false;
                   if (inputValue === "")
                   {
                       swal.showInputError("You need to write something!");
                       return false
                   }
                   alert(inputValue);
                   var form = $('#__AjaxAntiForgeryForm');
                   //form.append('<input type="hidden" name="name" value=' + inputValue + ' />');
                   var token = $('input[name="__RequestVerificationToken"]', form).val();
                   $.ajax({
                       url: ($(this).data('url')),// i tried to do ($(this).data('url'))+'?name='+inputValue but i got undefined id
                       type: "POST",
                       data: {
                           name: inputValue,
                           __RequestVerificationToken: token
                       },
                       success: SuccessCallback,
                       error: FailureCallback
                   });
               });
               function SuccessCallback(data) {
                   swal({
                       title: "Opération réussie",
                       type: "success"

                   }, function () {
                       NProgress.done();
                       location.reload();
                   });
               }
               function FailureCallback(data) {
                   swal("Good job!", "You clicked the button!", "error");
                   NProgress.done();
               }

           });});

这是我的控制器

        [HttpPost]
    [ValidateAntiForgeryToken]
    // POST: Dashboard/Rename/5
    public async Task<ActionResult> Rename(int id,[System.Web.Http.FromBody] string name)
    {
        Dashboard dashboard = await dbs.Dashboards.FindAsync(id);
        //dashboard.Visible = true;
        dashboard.TitreD = name.ToString();
        dbs.Entry(dashboard).State = EntityState.Modified;
        await dbs.SaveChangesAsync();
        return Json(new { success = true });
    }

我的视图中有一个隐藏表单

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "__AjaxAntiForgeryForm" })){
@Html.AntiForgeryToken()
}

正文内容为
名字|测试
__RequestVerificationToken|令牌

解决了我的问题 ($(this).data('url')) 没有给出正确的 url 因为它没有引用给 $("#renamedashboard") 谁有正确的 url

    <a id="renamedashboard" href="#" data-url=@Url.Action("Rename", "Dashboard",new {id=activedashboard.Idd}) >