1

So I do this post:

$http.post(Common.blog.save, { blog: blog })
    .then(saveBlogComplete)
    .catch(function(message) {

    });

And I get this in fiddler output:

{"blog":{"title":"Chicken Is Good","content":"#Chicken Is Good\n##Contents\n* Introduction\n* Factfile\n* Analysis\n* Evaluation\n* Conclusion\n###Introduction\n\n###Factfile","dateAuthored":"","userId":""}}

In my action:

[HttpPost]
public JsonResult Save(string blog)
{
    var desBlog = JsonConvert.DeserializeObject<BlogDto>(blog);

    return this.ExecuteService(() => this.blogService.Save(desBlog));
}

string blog is coming back null.... I'm not sure why this is happening?

I have done the following

  • Put breakpoint in JavaScript - data is getting populated
  • Reviewed Fiddler output - the data is the same as JavaScript obj
  • Put breakpoint in the Action - it's getting called, the HttpContext doesn't have any data about the POST data in it
2
  • 1
    Did you try stringifying the object? {blog: JSON.stringify(blog)} (uses the JSON2 js library) Commented Oct 21, 2014 at 18:41
  • i didn't, I will give it a go! Commented Oct 21, 2014 at 18:43

2 Answers 2

2

Your code will work without stringify function, if you change your mvc action parameter from String to Blog:

public class Blog
{
   public string Title {get; set;}
   public string Content {get; set;}
   public DateTime DateAuthored {get; set;}
   public long UserId {get; set;}
}

[HttpPost]
public JsonResult Save(Blog blog)
{

This happening because your blog model on server-side doesn't match to the structure of passing parameter from angular.

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

1 Comment

That will automatically create the blog object from the posted data, but I believe MVC still expects the data to be posted in string format.
0

I just needed to stringify the data!

$http.post(Common.blog.save, { blog: JSON.stringify(blog) })
    .then(saveBlogComplete)
    .catch(function(message) {

    });

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.