0

This is my action method which is supposed to receive a customer object and a string of json, I need both to be posted in same Action Method however,

public ActionResult CreateNewCustomer(Customers customers, string model)
{
  //....Codes omitted 
}

I tried posting using AJAX like here,

$.ajax({
  type: "POST",
   url: url,
   data: { customers: $("#formtopost").serialize(), model: JSON.stringify(jsonToPost) }, 
    success: function (data) {
    alert(data); 
    }
  });

e.preventDefault(); 

But with this approach The customers object is always being null, Either I have to exclude model string or customers object. Please help me how can I receive both in same Action Method like mentioned above.

Any suggestions will be appreciated.

15
  • 1
    your json parameter name and action one have to match, change in action parameter from customer to customers Commented May 17, 2016 at 10:22
  • @EhsanSajjad Ohh I am sorry, This was my mistake while posting question but it's sure both are same. Now I edited my question. Please help me. Commented May 17, 2016 at 10:25
  • You need to add [HttpPost] for your action method. Commented May 17, 2016 at 10:31
  • @Bharadwaj It is there and I am well receiving the model string but only customers is null. Commented May 17, 2016 at 10:32
  • 1
    @Bharadwaj no - that's not what [HttpPost] does. You only need this if there are other methods with the same name and in that case you want to add [HttpGet] to the get methods. To save you looking it up, the [HttpPost] (get/delete/etc) attributes limit the action to that verb, they do not say it's a post method - it's always a post method unless there's a restriction on it. Commented May 17, 2016 at 10:36

2 Answers 2

0

first you will define customer

var customer = {};

add data to the properties of customer like

customer.Name = "MyName";

and finally make ajax call to the controller

var Stringmodel = "*your string*";

$.ajax({
                method: "POST",
                url: "/*your controller*/ * your action*",
                contentType: "application/json",
                data: JSON.stringify({ 'customers': customer , 'model': Stringmodel })
        })
Sign up to request clarification or add additional context in comments.

1 Comment

add comment before marking negative so that i can improve my answer
-1

Append Querystring model to the url. The data being passed must be formtopost serialized value.

e.preventDefault(); 
$.ajax({
    type: "POST",
    url: url?model=jsonToPost,
    data: $("#formtopost").serialize(), 
    success: function (data) {
         alert(data); 
      }
});

6 Comments

Thanks for the answer, I appreciate. This seems working, But beside that It's not only the matter of model string, If I post excluding model, I mean only the form by serializing it's still null. What may be the cause, Is it because form could not be serialized. If so how can I debug that ?
what is null? model or customers?
customers is null, model is fine, OK for temporary purpose i removed model, Even only the form is also null do you have any idea ?
Might be issue with keyvalue pair being binded. Can you check in developer tools what is the keyvalue pair being sent?And does the keys match the Customer properties
can you pls expalain why it is downvoted? the above solution works when you need to pass complex object and string and that is what was expected.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.