0

I have this method in my controller:

[HttpPost]
public ActionResult Create(StatusMessage statusMessage, int foreignKey, int statusMessageType)
{
    //Do something
}

This is how my StatusMessage model looks like:

public abstract class StatusMessage 
{
    [Key]
    public int Id { get; set; }

    /// <summary>
    /// Message
    /// </summary>
    /// [Required]
    public string Description { get; set; }

    public DateTime CreationDate { get; set; }

    public int UseraccountId { get; set; }
    public virtual Useraccount Useraccount { get; set; }
}

And I'd like to send a post via jquery ajax to my controller:

function sendForm(message, foreignKey, statusMessageType, target) {
    var statusMessage = {
      Description: "This is a test message"
    };
    $.ajax({
        url: target,
        type: "POST",
        contentType: 'application/json',
        data: JSON.stringify({
            statusMessage: statusMessage,
            foreignKey: foreignKey,
            statusMessageType: statusMessageType
        }),
        success: ajaxOnSuccess,
        error: function (jqXHR, exception) {
            alert(exception);
        }
    });
}

But the POST isn't sent correctly. Through testing I figured already out that the problem is the StatusMessage class. If I replace it by a string, everything is working fine.

2 Answers 2

1

Why you have defined your class as abstract, controller won't be able to create its instance

Define you class as

 public class StatusMessage

for info http://www.codeproject.com/Articles/6118/All-about-abstract-classes

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

2 Comments

This has some technical background. This is the base class for some concrete status message classes. To determine which status message I want I'm using statusMessageType. Hoped this would work somehow.
You should create derived classes as per message type
0

Your ajax post is not working because StatusMessage is an abstract class and you (or the controller) cannot create an instance of it. You should either make it a concrete class or derive another class from it.

5 Comments

This has some technical background. This is the base class for some concrete status message classes. To determine which status message I want I'm using statusMessageType. Hoped this would work somehow.
As I've suggested, why don't you then create a concrete class of specific type (e.g. FailedStatusMessage, SuccessStatusMessage)?
I basically wanted to have only 1 method to create a status message (company status message, project status message).
How much difference does each type of message have? The downside of having one method is that you will introduce over/under posting if other type of your message have less/more fields than the others. You can always have different controller methods and then have a helper (private) method to centralize the logic of processing the input.
Thank you for your feedback. I made a seperate "view model" class which is concrete and contains only properties for the data I really want to send via the ajax post. Now it's working fine.

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.