简体   繁体   中英

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.

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.

Please suggest how we may pass it.

On option is to use FormData to build the model and add additional data

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,         
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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