1

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?

6
  • data: JSON.Stringify(disputeStatusObj) with the original action should work Commented Jun 21, 2018 at 12:20
  • Try adding [FromBody] before input parameter: public virtual ActionResult UpdateDisputeStatus([FromBody] DisputeUdpateStatusModel model) Commented Jun 21, 2018 at 12:22
  • data: {'disputeId': DisputeId, 'statusId': DisputeStatusId} try this. Commented Jun 21, 2018 at 12:23
  • Remove the contentType: "application/json; charset=utf-8", option (you are not stringifying the data, so use the default) Commented Jun 21, 2018 at 13:19
  • Hi all, I have tried all those ideas and I still get 500. What is confusing is that as soon as I take out the Data param, my controller code gets called. My model is structured:' public class DisputeUdpateStatusModel { public DisputeUdpateStatusModel(string disputeId, string statusId) { DisputeId = disputeId; StatusId = statusId; } public string DisputeId { get; set; } public string StatusId { get; set; } } ... very confused as to why it is so hard to send data in Ajax to MVC Commented Jun 21, 2018 at 13:49

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

@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?

8 Comments

Hi @PrateekDeshmukh I have tried that and it didnt work either, thants tho
@Mike did you try writing just action result without any code into it? You said it is giving 500 error which means internal server error. If the route is not found it should get 404 not found. It may be the case that some code is crashing inside the action result.
Yes, I tried that and if you read my post, the Ajax call works successfully when there is no data passed and no model in the controller. thanks
@MikeCave I just edit my answer, by making some changes and verified in sample mvc app, can you please confirm if your code looks like this?
Hi Prateek, thanks very much for the effort you have put in, I have just posted my answer as I managed to solve the issue ( see below). Definitely Plus one for giving me another solution. I will give it a go and report back. many thanks
|
0

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!

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.