15

I have seen a few questions on here related to the a similar issue, I have read them, followed them, but still i have the same problem.

I am basically creating an object in javascript and trying to call a method on the controller that will return a string of html. Not JSON.

I've been playing around with dataType and contentType but still no joy. So apologies if the code snippets are a bit messy.

Build the object in JS.

function GetCardModel() {
    var card = {};
    card.CardTitle = $("#CardTitle").val();
    card.TopicTitle = $("#TopicTitle").val();
    card.TopicBody = $("#TopicBody").data("tEditor").value();
    card.CardClose = $("#CardClose").val();
    card.CardFromName = $("#CardFromName").val();
    return card;
}

Take a look at the object - all looks good and as it should in JSON.

var model = GetCardModel();
alert(JSON.stringify(GetCardModel()));

Make the call...

$.ajax({
            type: "POST",
            url: "/Postcard/Create/Preview/",
            dataType: "json",
            //contentType: "application/json",
            data: GetCardModel(),
            processData: true,
            success: function (data) {
                alert("im back");
                alert(data);
            },
            error: function (xhr, ajaxOptions, error) {
                alert(xhr.status);
                alert("Error: " + xhr.responseText);
                //alert(error);
            }
        });

Always when I step into the controller, the object is ALWAYS there, but with null values for all the properties.

2
  • Well, don't edit the mistake out of your question. Makes it tougher to see what Darin found! (data above was date in the first version, natch) Commented Dec 13, 2013 at 18:54
  • 1
    That wasn't the problem, it was a typo in the question. Always read the comments on answers Commented Dec 13, 2013 at 22:33

1 Answer 1

27

The parameter name should be data, not date:

$.ajax({
    type: 'POST',
    url: '/Postcard/Create/Preview/',
    dataType: 'json',
    data: {
        CardTitle: $("#CardTitle").val(),
        TopicTitle: $("#TopicTitle").val(),
        TopicBody: $("#TopicBody").data("tEditor").value(),
        CardClose: $("#CardClose").val(),
        CardFromName: $("#CardFromName").val()
    },
    success: function (data) {
        alert('im back');
        alert(data);
    },
    error: function (xhr, ajaxOptions, error) {
        alert(xhr.status);
        alert('Error: ' + xhr.responseText);
    }
});

which will successfully call the following controller action and the action parameter will be properly bound:

[HttpPost]
public ActionResult Preview(Card card) { ... }

with the model below:

public class Card
{
    public string CardTitle { get; set; }
    public string TopicTitle { get; set; }
    public string TopicBody { get; set; }
    public string CardClose { get; set; }
    public string CardFromName { get; set; }
}
Sign up to request clarification or add additional context in comments.

9 Comments

yeah that was a typo...sorry.
@SteveCI, OK please see my update, but even with the external function it worked fine for me.
my model is that, but with a couple extra props. OK, it is now passing to the model to the controller, but I needed to tweak it like you said except to change dataType to text and add in contentType to application/json - which is the one that was stopping the data being passed. removed processData - not sure what that does or defaults to.
for the data i now have JSON.stringify(GetCardModel()). do i need to stringify ?? well it works so i guess so.
I just had public string PropertyName instead of public string PropertyName { get; set; } and that was the issue in my case. Thanks!
|

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.