【问题标题】:reading ajax request body values by name with asp.net mvc 5 action controller使用 asp.net mvc 5 动作控制器按名称读取 ajax 请求正文值
【发布时间】:2015-04-13 15:03:02
【问题描述】:

我想访问 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|令牌

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc-5 sweetalert


    【解决方案1】:

    解决了我的问题是 ($(this).data('url')) 没有提供正确的 url,因为它没有引用具有正确 url 的 $("#renamedashboard")

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

    【讨论】:

    • 我不明白你说什么-1
    猜你喜欢
    • 2014-08-03
    • 2016-12-23
    • 1970-01-01
    • 2012-03-23
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多