0

This is similar to: another stack overflow q

I have tried something similar to this question:

var MyItem= { "msg": "hello word!" };

$http.post("/MyController/testPost", MyItem).success(function (data) {
    Alert(ok)
})

I have also tried creating MyItem as a Javascript object.

Then in MyController:

[post]
public void testPost(MyItem MyItem)
{

 }

Where MyItem, looks like this:

public class MyItem
{
    public string msg; 
}

The action in the controller is always hit in all scenarios, but MyItem is always has a null value for msg. Am I missing something? Is there a better way of doing this?

7
  • 1
    Shouldn't the signature for testPost be testPost(MyItem RiskItem) and shouldn't your post be passing RiskItem instead of MyItem? Like $http.post("/MyController/testPost", RiskItem) ? Commented Mar 26, 2015 at 17:09
  • Thanks - that was a typo in writing my question - the problem still stands though Commented Mar 26, 2015 at 17:12
  • you said the Action gets hit, but does that particular method get hit? If you set a breakpoint inside testPost does it get hit? Commented Mar 26, 2015 at 17:15
  • Yes, it does. Just the msg value in the MyItem parameter is null. Actually, putting a breakpoint in the javacript post action, I can see that the value of msg is null before it is sent Commented Mar 26, 2015 at 17:19
  • 1
    That last comment is actually quite vital to getting a good answer to this question. Please consider editing it into the question, as it is a JavaScript problem, not an ASP.NET problem. Commented Mar 26, 2015 at 17:34

2 Answers 2

3

For me it started to work when I had changed the "msg" field to property. No additional attributes except [HttpPost] for the controller.

    public class MyItem
    {
        public string msg { get; set; }
    }

    [HttpPost]
    public ActionResult testPost(MyItem MyItem)
    {

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

1 Comment

I just came in this morning and tried that (before looking at your post). Yes, that works, it needs to be a property, not a field. Thanks!
1

I would try two options...

Try changing var MyItem= { "msg": "hello word!" }; to..

var MyItem = { msg: "hello word!" };

or

var MyItem = {};
MyItem.msg = "hello world!";

6 Comments

Also strip out all the wcf attributed sh!t and put [HttpPost] on your action
I have tried these and have just tried them again, but they also have a null msg even on the post method on the way in.
haha, those attributes are also gone Matt - I just tried those in because nothing seems to work
@Sam have you looked at this -- victorblog.com/2012/12/20/…
I have tried this, but it does not work unfortunately - the config updates correctly, but the same issues remain.
|

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.