0

JsonConvert.SerializeObject(addToBasketView) を使用するときに問題が発生しました。渡すリストが気に入らないようです-リストを渡すまでは正常に機能していました-リストを渡すことができませんか?

これが私のコードです:

コントローラ:

//We need a list of Automation scripts - this was in Apollo but now we need to pass it in teh message
           var automationList = AutomationHelper(retailerId, productId);

 var addToBasketView = new AddToBasketView
            {
                Url = retailerProduct.DeepLink,
                Password = (password),
                Username = (username),
                RetailerProductJson = new RetailerProductJson(retailerProduct),
                WidgetImpressionId = id,
                Quantity = qty,
                MessageId = messageId,
                AutomationList = automationList
            };


            // now turn it into a string
            var json = JsonConvert.SerializeObject(addToBasketView);

Addtobasketview.cs:

using System;
using System.Collections.Generic;

namespace WidgetData
{
    /// <summary>
    /// This is the view that we will give to the AddToBasketForm
    /// </summary>
    [Serializable]
    public class AddToBasketView
    {


        /// <summary>
        /// The Username for the retailer
        /// </summary>
        public String Username { get; set; }

        /// <summary>
        /// The password for the retailer
        /// </summary>
        public String Password { get; set; }

        /// <summary>
        /// The URl of the thing they want added to the site
        /// </summary>
        public String Url { get; set; }

        /// <summary>
        /// The retailer product selected - from this I can get the retailer and the product.
        /// </summary>
        public RetailerProductJson RetailerProductJson { get; set; }

        /// <summary>
        /// The widget impression id so that we can attach to the addTobaskets table for recording purposes
        /// </summary>
        public int WidgetImpressionId { get; set; }

        /// <summary>
        /// set the quantity
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// The MessageId this is so we can identify it when waiting for a response
        /// </summary>
        public String MessageId { get; set; }

        /// <summary>
        /// Automation script list
        /// </summary>
        public List<AutomationStepScript> AutomationList { get; set; }

    }
}

編集: AutomationStepScript.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace WidgetData
{
    /// <summary>
    /// Extends the class created by Linq To SQL, so that we can control in our models section the validity of the data
    /// </summary>
    [MetadataType(typeof(AuotmationStepScriptValidation))]
    public partial class AutomationStepScript
    {
        /// <summary>
        /// Override the to string so that we can use in the delete method to capture details of this record before it is deleted
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
           return string.Format("AutomationStepScript Id = {0}, AutomationStepId = {1}, Sort Order = {2}, Description = {3}, Script = {4}, Evaluate = {5}", Id,AutomationStepId,SortOrder,Description,Script,Evaluate);
        }
    }

    /// <summary>
    /// Class to validate my AuotmationStepScript
    /// </summary>
    public class AuotmationStepScriptValidation
    {
        /// <summary>
        /// AutomationId
        /// </summary>
        [Required(ErrorMessage = "AutomationStepId is a required field")]
        [Range(1, int.MaxValue, ErrorMessage = "AutomationStepId must be valid")]
        public int AutomationStepId { get; set; }

        /// <summary>
        /// Display SortOrder
        /// </summary>
        [Range(0, int.MaxValue, ErrorMessage = "SortOrder must be valid")]
        public int SortOrder { get; set; }

        /// <summary>
        /// Description 
        /// </summary>
        [StringLength(256, ErrorMessage = "Description maximum length is 256", MinimumLength = 1)]
        public String Description { get; set; }

        /// <summary>
        /// Script
        /// </summary>
        [AllowHtml]
        [StringLength(8000, ErrorMessage = "Script maximum length is 8000")]
        public String Script { get; set; }

        /// <summary>
        /// Evaluate
        /// </summary>
        public int Evaluate { get; set; }
    }


}

AutomationHelper (リストを取得するためにコントローラで呼び出されます:

 /// <summary>
        /// The helper function to fetch the data so that we can get the scripts for each step for this automation (which we will find from the localParams)
        /// </summary>
        /// <param name="retailerId"> </param>
        /// <param name="productId"> </param>
        /// <returns>WE return a List of AutomationStepScripts, this object will be selected further with Lambda expression"></returns>
        public List<AutomationStepScript> AutomationHelper(int retailerId, int productId)
        {
            List<AutomationStepScript> automationStepScripts = null;
            var automationStepScriptRepository = new AutomationStepScriptRepository();
            var productRepository = new ProductRepository();
            var retailerCategoryRepository = new RetailerCategoryRepository();
            var product = productRepository.GetProduct(productId);
            var categoryId = product.CategoryId;
            var retailerCategory = retailerCategoryRepository.GetRetailerCategoryByRetailerCategory(retailerId, categoryId);

            // DO we have a retailer category?
            if (retailerCategory != null)
            {
                // Yes, without a valid retailer category we cannot possibly find the automation
                // Now here we have the RetailerCategory. The next question is Does this RetailerCategory HAVE an automation set up for it
                if (retailerCategory.AutomationId != null)
                {
                    // Yes, we have an automation. So lets get all of the Scripts for all of the steps for this automation
                    // Get All scripts for all steps for this automation.
                    automationStepScripts = automationStepScriptRepository.GetAllAutomationStepScriptsForAutomation((int)retailerCategory.AutomationId).ToList();
                }
            }
            return automationStepScripts;
        }

後で使用する SQS で使用しているメッセージの本文になるので、シリアル化しています。

さらに情報が必要な場合はお知らせください。

ありがとう。

4

0 に答える 0