繁体   English   中英

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

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

我想访问jquery ajax发布请求正文以获取我的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,因为它不符合具有正确URL的$("#renamedashboard")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM