Mike Cave
Mike Cave

Reputation: 147

Using Ajax to POST to ASP.Net controller - Data for Post incorrect

This is my DisputeController method stub:

[HttpPost]
public virtual ActionResult UpdateDisputeStatus(DisputeUdpateStatusModel model)
{//some code

Here is my Ajax call:

var url = '/dispute/UpdateDisputeStatus';
var disputeStatusObj = {
    DisputeId: id,
    DisputeStatusId: selectedValue
}

$.ajax({
    url: url,
    cache: false,
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    data: disputeStatusObj,       
    success: function (data) {
        alert('Status Changed Successfully');
    },
    error: function (e) {
        alert('Error: ' + e.status);
    }
});

I know the routing works, as without using the data parameter the code enters my method (obviously without the model in parameters)

I have tried the following data formats:

data: {'DisputeId': DisputeId, 'StatusId': DisputeStatusId},  
data: {disputeStatusObj},
data: JSON.Stringify(disputeStatusObj)

using controller methods:

[HttpPost]
public virtual ActionResult UpdateDisputeStatus(string disputeId, string statusId)


[HttpPost]
public virtual ActionResult UpdateDisputeStatus(modelname model)

None of which work. I get Not found errors or 500's.

Bearing in mind that I know the routing is correct, when I send the request with no data, so what am I missing here?

Am I declaring the controller incorrectly?

Upvotes: 1

Views: 99

Answers (3)

Prateek Deshmukh
Prateek Deshmukh

Reputation: 135

@Mike I tried below code

   public class DisputeUpdateStatusModel
    {
        public string DisputeId { get; set; }
        public string DisputeStatusId { get; set; }
    }
    public class DisputeController : Controller
    {

        [HttpPost]
        public virtual ActionResult UpdateDisputeStatus(DisputeUpdateStatusModel model)
        {
            return new ContentResult() { Content = "OK" };
        }
    }

script on view as:

<script type="text/javascript">
    var disputeStatusObj = {}
     disputeStatusObj.model = {
        DisputeId: 1,
        DisputeStatusId: 1
    }
    var url = '/dispute/UpdateDisputeStatus';
    $.ajax({
        url: url,
        cache: false,
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(disputeStatusObj),
        success: function (data) {
            alert('Status Changed Successfully');
        },
        error: function (e) {
            alert('Error: ' + e.status);
        }
    });

</script>

Please see.

Working screenshot of browser

If this is not working, can you please show Model class?

Upvotes: 1

Mike Cave
Mike Cave

Reputation: 147

thanks for all your input. My solution was a combination of the answers above. I was unable to get my code to model bind, so I went back to basics. I took out the contentType: "application/json; charset=utf-8", as suggested by Stephen Muecke, and ditched the model in my controller and replaced it with string disputeId, string statusId and as suggested by Er Pravin Suthar ensured the parametes were called the same thing. Also to note, the data section was sent as data: { disputeId: disputeId, statusId: statusId } with no quotes around the parameter names. So I ended up with:

var statusId = statusDropdown.value; 
var disputeId = $("#disputeId").val();      

$.ajax({
    url: "/Dispute/UpdateDisputeStatus",
    data: { disputeId: disputeId, statusId: statusId },
    type: 'POST',
    success: function (data) {
        alert('Success');
    },
    error: function (e) {
        alert('Status not changed' + e.responseText + '  : ' + e.status);
    }       
});

and the Controller structure is:

    [HttpPost]
    public virtual ActionResult UpdateDisputeStatus(string disputeId, string statusId)

Thanks Again for all your input!

Upvotes: 0

Er Pravin Suthar
Er Pravin Suthar

Reputation: 11

Verify your model that should be the same name as well as the data type because of while post the value from Jquery that values are not mapping with the property of the model.

Upvotes: 1

Related Questions