简体   繁体   English

Asp.net MVC:控制器,第一个参数为json,第二个参数为字符串,来自View

[英]Asp.net MVC: controller with first parameter as json and second parameter as string from View

We have a situation where we would like controller to get First parameter as json (model) as second parameter as some additional data other than model (such as Flag, source control from where event is driven etc.), we have tried tweaking with jQuery but all ended up error shown in the browser inspect element. 我们有一种情况,我们希望控制器将第一个参数作为json(模型)作为第二个参数作为除模型以外的一些其他数据(例如Flag,驱动事件的源代码控制等),我们尝试使用jQuery进行调整但所有最终错误均显示在浏览器inspect元素中。

We have our controller typically like this: 我们的控制器通常如下所示:

public async Task<ActionResult> Foo(Bar b, string additionaldata)
{
    if (additionaldata="Deleted")
    {

    }
    else if (additionaldata="Favorite")
    {

    }
}

And inside view its something like this: 并在内部查看如下内容:

$("#delete").click(function () {
                    $.ajax({
                        url: "/Index/Foo",
                        type: "POST",
                        data: $("#myform").serialize(), 
                        dataType: "json"
                    }).done(function (model) {
                        $("#Foo_Id").val(model.Foo.Id);
                    });
                });

As far as model is concerned, this jQuery is working fine, but as far as we try to add some additional parameter, we are clueless. 就模型而言,此jQuery工作正常,但就我们尝试添加一些其他参数而言,我们一无所知。

Please suggest how we may pass it. 请建议我们如何通过它。

On option is to use FormData to build the model and add additional data On选项是使用FormData构建模型并添加其他数据

var formdata = new FormData($('#myform').get(0)); // serialize the form
formdata.append('additionaldata', 'Favorite'); // add additional properties
$.ajax({
  url: '@Url.Action("Index", "Foo")',
  type: 'POST',
  data: formdata,
  processData: false,
  contentType: false,         
});

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

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