简体   繁体   English

使用jQuery AJAX将多个变量POST到ASP .NET MVC控制器

[英]Using jQuery AJAX to POST multiple variables to an ASP .NET MVC Controller

I have a controller to which I want to POST 2 items via AJAX: a complex object (my entire viewmodel), and an integer (the id of a particular row). 我有一个控制器,我想通过AJAX发布2个项目:一个复杂的对象(我的整个viewmodel)和一个整数(特定行的id)。 This particular project is in VB .Net, but if someone can answer this in C#, that would be fine (I know both languages fairly well). 这个特殊项目是在VB .Net中,但是如果有人能用C#回答这个问题,那就没关系了(我对这两种语言都很了解)。 Either language would work. 这两种语言都可行。

I can POST the viewmodel to the controller without any problem. 我可以将viewmodel POST到控制器没有任何问题。 Once I try to include the integer, the controller can no longer route the request. 一旦我尝试包含整数,控制器就不能再发送请求了。 I know that it is probably an issue of how I am formatting the data that I POST, but I haven't been able to figure out exactly what I need to do. 我知道这可能是我如何格式化POST的数据的一个问题,但我无法确切地知道我需要做什么。

My controller action looks like: 我的控制器动作如下:

<HttpPost>
Public Function UpdateFromDate(viewModel As RetirementBenefitEstimateViewModel, estimateId) As ActionResult
    If viewModel IsNot Nothing AndAlso viewModel.Estimate IsNot Nothing AndAlso viewModel.Estimate.RetirementBenefitsEstimates IsNot Nothing Then

      For Each item In viewModel.Estimate.RetirementBenefitsEstimates.Where(Function(est) est.EstimateId = estimateId)
        ' this is where I update the affected row
        item.UpdateFromDate(viewModel.DateOfBirth, viewModel.EmploymentStartDate, viewModel.PersonId)
      Next item
    End If

    ' Get the previous ViewModel from session
    PerformSessionAction(Of RetirementBenefitEstimateViewModel)(SessionConstants.RetirementEstimate, currentEstimate, SessionAction.GetVar)
    ' update it's .Estimate property
    currentEstimate.Estimate = viewModel.Estimate
    ' save the updated ViewModel to session
    PerformSessionAction(Of RetirementBenefitEstimateViewModel)(SessionConstants.RetirementEstimate, currentEstimate, SessionAction.SetVar)
    ' finished!
    Return New HttpStatusCodeResult(HttpStatusCode.OK)
End Function

The jquery AJAX call in my view looks like: 我视图中的jquery AJAX调用如下所示:

$.ajax({
        type: "POST",
        url: '@Url.Action("UpdateFromDate")',
        data: { viewModel : model, estimateId : 3 }
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        cache: false,
        success: function (msg) {
          //alert(JSON.stringify(msg));
          return true;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          //alert(errorThrown);
          return false;
        }
      });

How can I POST my viewmodel and the integer (hard-coded to 3 in this example)? 如何发布我的viewmodel和整数(在本例中硬编码为3)?

Scottie's post got me on the right track. 斯科蒂的帖子让我走上正轨。 I was tempted to mark it as the answer, but it had one tiny problem. 我很想把它标记为答案,但它有一个小问题。 The integer would POST correctly, but the viewmodel started showing up as null in the controller. 整数将正确POST,但viewmodel在控制器中开始显示为null。 All that was needed to fix this was a simple JSON.parse call. 修复此问题所需的只是一个简单的JSON.parse调用。

My AJAX call ends up looking like: 我的AJAX调用最终看起来像:

var params = {
    viewModel: JSON.parse(model),
    estimateId: 3
};

$.ajax({
    url: '@Url.Action("UpdateFromDate")',
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(params),
    async: false,
    cache: false,
    traditional: true,
    contentType: 'application/json',
    success: function  (msg) {
      //alert(JSON.stringify(msg));
      return true;
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      //alert(errorThrown);
      return false;
    }
  });   

Try this: 尝试这个:

var params = {
    viewModel: model,
    estimateId: 3
};

$.ajax({
    url: '@Url.Action("UpdateFromDate")',
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(params),
    async: false,
    cache: false,
    traditional: true,
    contentType: 'application/json',
    success: function  (msg) {
      //alert(JSON.stringify(msg));
      return true;
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      //alert(errorThrown);
      return false;
    }
  });   

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

相关问题 如何使用AJAX将数据发布到ASP.NET MVC控制器? - How to post data to ASP.NET MVC controller using AJAX? 如何使用ajax jquery将多个对象从视图发布到dot net core 6 MVC控制器 - How To Post Multiple Objects from View To dot net core 6 MVC Controller using ajax jquery ASP.NET Core MVC Ajax发布到具有多个参数的控制器返回错误的请求 - Asp.net core mvc ajax post to controller with multiple parameters return bad request 将多个参数从Ajax Post传递到ASP.NET MVC控制器 - Pass multiple parameters from ajax post to asp.net mvc controller ASP.NET MVC 4表单发布变量在接收控制器中为null - ASP.NET MVC 4 form post variables are null in receiving controller ASP.NET 5 / MVC 6 Ajax将模型发布到Controller - ASP.NET 5 / MVC 6 Ajax post Model to Controller Ajax 发布到 ASP.net MVC 控制器 - 对象属性为空 - Ajax post to ASP.net MVC controller - object properties are null 将ajax数据发布到ASP.NET MVC Controller - Post ajax data to ASP.NET MVC Controller 如何使用 AJAX POST 对象并使用 ASP.NET Core MVC 控制器获取? - How to POST object using AJAX and get with ASP.NET Core MVC controller? 琐碎的JQuery $ .ajax Json发布到ASP.NET MVC2控制器:无法让Controller查看JSON(或其他任何内容) - Trivial JQuery $.ajax Json Post to ASP.NET MVC2 controller: Cannot get Controller to see JSON (or anything)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM