kielou
kielou

Reputation: 437

Proper way to handle the code

how can I properly handle the code using var from jquery, how can I use it as a parameter in @url.action correctly. I tried

url: '@Url.Action("CreateDisease", "DiseaseLists", new {'"+diseaseID+"', '"+ assessmentID+"' })',

but it didn't work

$(document).ready(function () {
    $('#btn-disease').click(function () {
        var diseaseID = $('#DiseaseID').val();
        var assessmentID = $('#AssessmentID').val();

        $.ajax({
            type: "POST",
            dataType: "Json",
            data: {diseaseID:'" + diseaseID + "',assessmentID: '" + assessmentID + "' },
            url: '@Url.Action("CreateDisease", "DiseaseLists", new {//parameter here* diseaseID, assessmentID  })',


            success: function (f) {
                if (f.Result == "success") {
                    alert("success!");
                }
                else {
                    alert("Disease Already Added");
                }
                alert("????????????????????");
            }
        })
    })
})

Upvotes: 0

Views: 32

Answers (2)

avi
avi

Reputation: 912

Assuming that your controller is DiseaseLists(weird name for a controller if you ask me) and the method is CreateDisease that has exactly the parameters diseaseID and assessmentID, you can write something like

$(document).ready(function () {
$('#btn-disease').click(function () {
    var diseaseID = $('#DiseaseID').val();
    var assessmentID = $('#AssessmentID').val();       
    $.ajax({
        type: "POST",
        dataType: "Json",
        data: {'diseaseID': diseaseID ,
                'assessmentID': assessmentID 
        },
        url: '@Url.Action("CreateDisease", "DiseaseLists")',
        success: function (f) {
            if (f.Result == "success") {
                alert("success!");
            }
            else {
                alert("Disease Already Added");
            }
            alert("????????????????????");
        }
    })
})

})

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337570

You only need to provide the URL in the url parameter. The data in the request should be provided to the data property of $.ajax as an object. Your syntax for that is a little off. Try this:

url: '@Url.Action("CreateDisease", "DiseaseLists")',
data: { 
  diseaseID: diseaseID, 
  assessmentID: assessmentID
},

Upvotes: 1

Related Questions