Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Karel-Jan Van Haute 5 posts 85 karma points
    Jan 30, 2018 @ 16:26
    Karel-Jan Van Haute
    0

    PublishedContentModel used in UmbracoApiController gives NULL

    I have the following

    public class NewsletterApiController : UmbracoApiController
    {
        public string SaveOptin(NewsletterOptin optin)
        {
            return "ok";
        }
    }
    

    NewsletterOptin is a generated Model through the LiveAppData. When I send data through ajax to the controller I get "Object reference not set to an instance of an object." for the parameters of the model.

    The error occurs on this generated code:

            ///<summary>
        /// Email
        ///</summary>
        [ImplementPropertyType("email")]
        public string Email
        {
            get { return this.GetPropertyValue<string>("email"); }
        }
    

    Is this not possible? Or am I doing something wrong?

    I send my data like so:

    var optin = new Object();
      optin.Email = email;
      optin.Language = culture;
      optin.Professional = true;
      $.ajax({
        method: "POST",
        url: "/Umbraco/Api/NewsletterAPI/SaveOptin",
        contentType: "application/json; charsect=utf-8",
        data: JSON.stringify(optin)
      }).done(function (d) {
        console.log(d);
      }).fail(function(e){
        console.log(e);
      });
    
  • Steven Harland 78 posts 518 karma points c-trib
    Jan 30, 2018 @ 16:51
    Steven Harland
    0

    You cannot use the Models Builder models in this context. They are meant to provide read-only access to published content (notice how in the generated property only the get is implemented and not the set).

    What you need to do is create your own model class containing all the properties you want to bind and then use that as the parameter to the method.

    Also, you may want to reconsider saving these form submissions as content nodes (I'm guessing that's what you are trying to do) for the reasons mentioned here: https://our.umbraco.org/documentation/reference/Common-Pitfalls/#using-umbraco-content-items-for-volatile-data

    Have a look at using PetaPoco (shipped with Umbraco) to save these to a custom database table. You could then use either Fluidity or UI-O-Matic to make the data available in the backoffice.

Please Sign in or register to post replies

Write your reply to:

Draft