2

I am sending data from angularJS to MVC controller (I see that angularJS function has argument value, but on MVC controller side that value is null, and I have no clue why is that so...)

//function in angularJS controller

//initialize model
var model = {};
model.Name = "some string";
save(model, onSaveSuccess, onSaveError);
...

//post to MVC controller...
function save(model, success, error) {
    //name **has** value!
    return $http.post('/Test/Save', { params: { model: model } })
        .success(function (data) { errorHandler.jsonError(data, success, error); })
        .error(function (data, status) { errorHandler.httpError(data, status, error); });
}

//MVC controller side (TestController)

[HttpPost]
public JsonResult Save(SaveModel model) //SaveModel is a class that has Name property of type string
{
    //when debug reaches -> name is null...
    //...
}

And in that solution, there are other cases where data are saved seemingly same way and it works there... Any idea what to check, or what might be wrong? I'm new to angularjs, maybe I am missing something really simple...

P.S.

1) If I change "params:" to "data:" as suggested result is the same

2) If I remove "params:" then I get 500 error. I.e. change: { params: { model: model } } -> { model: model }

PROBLEM/Solution

So, the problem turned out to be both client and server side

1) on client side I had to change { params: { model: model } } -> model as suggested by StriplingWarrior

2) on server side my model was inheriting from BaseModel which was setting some localization related stuff in it's constructor... and it was failing on my machine. The problem was that when debuging MVC controller -> this BaseClass code was performed before it, so it was hidden from me. I'm not yet common with browser Developer Tools, so I was not looking for information in the right place -> when I turned to Network tab (Chrome Developer tools), I could get exception information for my service call.

Here is a similar case answered on SO. I seem to follow same rules without success

2
  • try renaming params: to data:. params: is for GET request Commented Apr 5, 2018 at 15:00
  • @S.Dav I I change params: to data : outcome is the same... on MVC controller side I receive name with null value. If I do as @StripplingWarrior suggested, then I get 500 error Commented Apr 5, 2018 at 17:08

3 Answers 3

2

The second argument to post is the actual data payload that you're posting, so you need to get rid of the params property and promote its object to the top level.

function save(name, success, error) {
    //name **has** value!
    return $http.post('/Test/Save', { name: name })
        .success(function (data) { errorHandler.jsonError(data, success, error); })
        .error(function (data, status) { errorHandler.httpError(data, status, error); });
}

Update

Based on your updated code, you should be passing the model directly as the body of your request:

function save(model, success, error) {
    //name **has** value!
    return $http.post('/Test/Save', model)
        .success(function (data) { errorHandler.jsonError(data, success, error); })
        .error(function (data, status) { errorHandler.httpError(data, status, error); });
}
Sign up to request clarification or add additional context in comments.

5 Comments

hmm, after I change it that way I get Internal server error 500
@Prokurors: Maybe try changing your input from a string to a model class that has a Name property on it. The [FromBody] attribute that Võ Quang Hòa recommended might also help.
I changed it to model with name property - now in controller I get this model, but name property is null
@StriplingWarrior .sucess and .error have been removed in the latest version of angularJs and will cause your app to crash. FYI.
@StriplingWarrior - I found out why after using your recommended solution I got a 500 Internal server error. There was a BaseModel class from which my model was inheriting, and this base model was having problems on my machine (related to localization) - when I fixed that issue, your solution did work for me. Thanks!
-1

I am almost 100% sure you need to use JSON.stringify() method on your name object, as passing it directly just like that will not put your string in apostrophes, which is needed for MVC to treat this value as string object.

Please look at what exactly is sent by browser in Developer tools' Network card to confirm my theory.

5 Comments

Well, whoever downvoted my answer, maybe you should comment why?
JSON.stringify is not needed, you can even send complex object without explicitly calling JSON.stringify
@CamiloTerevinto yes, for complex objects it works, but I had such issue with Angular 5, that when I just put string, JS treated it like JSON string and didn't encapsulate in " "
I said "even complex object". And this is AngularJS, far from Angular 5
Yeah, I found plunker with AngularJS and tried it. Neither of versions put the data into " ". The only way it worked, was to manually put it like: '"' + testValue + '"'
-1

I'm not sure it's work but you can try to change this

public JsonResult Save(string name) 

To

public JsonResult Save([FromBody]string name) 

When the param name is not null, you can check it's value and change from the angular part accordingly

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.