0

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

            }
        });

Method

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

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...

2
  • 1
    Seems weird that you are serializing an entire form into one variable. Did you look to see what is being sent up in the Ajax request in the network tab of the console? Commented Sep 2, 2015 at 12:27
  • Is there a particular reason you chose the later of two near-identical answers and only chose to comment on the other one? Commented Sep 2, 2015 at 18:03

2 Answers 2

5

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,
    ....
Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't he change contoller as well? I mean add aracid property to model?
No. On its own $("#RentForm").serialize() will bind to Kiralayan kisi, and this just adds ..&aracid=someValue to the form data so it will bind to the additional parameter
Then could you tell me what will happaned if ViewModel have aracid property and controller have same extra parameter aracid?
Hmm, I've never tested that :), but I would guess only one would be bound (the DefaultModelBinder ignores subsequent name/value pairs with the same name unless the property is IEnumerable) but I'm not sure. Something to test tomorrow. I'll let you know.
@teo van kot: If the name occurs in the model and a parameter, it will map to both the model property and the extra parameter. My own answer updated to include this little factoid.
1

MVC will map the object for you, so you might as well skip the extract nesting of the form within the object.

Notes:

  • If aracid is also a property in the model, it will map to both the property and the extra parameter.
  • Using push on the serialise() collection is more maintainable than the alternative of concatenating strings before the serialize() call.

e.g.

 var Kiralayan = $("#RentForm").serialize();            
 // Add the extra non-form parameter
 Kiralayan.push({name: 'aracid', value: P.AracID});

Full example:

    function Kaydet() {
        var Kiralayan = $("#RentForm").serialize();            
        // Add the extra non-form parameter
        Kiralayan.push({name: 'aracid', value: P.AracID});         
        console.log(params);

        $.ajax({
            type: "POST",
            url: '@Url.Action("Save","AracKirala")',
            data: Kiralayan,
            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();
                }

            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.