MVC jQuery的阿贾克斯后返回nulljQuery、MVC、null、阿贾克斯后

2023-09-10 21:53:20 作者:叫醒自己的不是闹钟是梦想

我一直在尝试后2 parameteres ... 这是阿贾克斯code

I have been trying to post two parameteres... This is the ajax code

function Kaydet() {
        var params = {};
        var Kiralayan = $("#RentForm").serialize();            
        params.kisi = Kiralayan ;
        params.aracid = P.AracID;           
        console.log(params);

        $.ajax({
            type: "POST",
            url: '@Url.Action("Save","AracKirala")',
            data: params,
            dataType: "text",
            success: function (response) {
                if (response != "OK") {
                    alert("Kayıt yapılamadı.");
                }
                else {
                    document.getElementById("RentForm").reset();
                    alert("Kayıt başarıyla gerçekleştirildi.");
                    $("#myModal").modal('hide');
                    Ara();
                }

            }
        });

方法

public ActionResult Save(Kiralayan kisi = null, int aracid = 0)
    {

问题是AJAX帖aracidcorretly但kisi变成空当方法触发时... 我试着不张贴aracid与kisi,让阿贾克斯贴好一个参数kisi,但不工作在一起...

the problem is ajax posts "aracid" corretly but "kisi" turns null when the method is trigged... I tried not to post "aracid" with "kisi" so ajax posted well for one parameter "kisi", but doesnt work together...

推荐答案

如果您序列化的形式,那么你可以添加额外的值,它与 .PARAM()功能

If you serializing the form, then you can add additional values to it with the .param() function

var data = $("#RentForm").serialize() + '&' + $.param({ 'aracid': AracID }, true);

$.ajax({
    type: "POST",
    url: '@Url.Action("Save","AracKirala")',
    data: data,
    ....