Sitecore MVC模型在回发后丢失

时间:2015-09-28 21:46:39

标签: sitecore sitecore8 sitecore-mvc

我有一个页面/注册,其中包含两个表单。

一个用于注册,另一个用于登录。注册表单是一个控制器呈现,登录表单是另一个控制器呈现。

当我加载/注册页面时,页面正确加载并填充模型。

单击“提交”按钮时,“发布”操作将触发,模型会突然重置为其默认状态。该模型是否仍然不需要重新填充就可以填充?

我的模特:

using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Attributes;
using Sitecore.Data.Items;
using Sitecore.Mvc.Presentation;
using System;
using System.Collections.Generic;

namespace MySite.Models.Items
{

    public class Registration: IRenderingModel
    {
        public Registration()
        {
            FirstName = string.Empty;
            LastName = string.Empty;
            Email = string.Empty;
            JobTitle = string.Empty;
            Company = string.Empty;
            Industry = string.Empty;
            SubIndustry = string.Empty;
            RevenueRange = string.Empty;
            Country = string.Empty;
            StateProvince = string.Empty;
            MarketoTracking = string.Empty;
            ReturnUrl = string.Empty;
        }

        public Rendering Rendering { get; set; }
        public Item Item { get; set; }
        public Item PageItem { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string JobTitle { get; set; }
        public string Company { get; set; }
        public string Industry { get; set; }
        public string SubIndustry { get; set; }
        public string RevenueRange { get; set; }
        public string Country { get; set; }
        public string StateProvince { get; set; }
        public string MarketoTracking { get; set; }
        public string ReturnUrl { get; set; }


        #region "Country List"
        public IEnumerable<CountryChildItem> CountryList { get; set; }
        public string CountryChildMenu { get; set; }

        [SitecoreType(AutoMap = true)]
        public class CountryItem
        {
            public virtual IEnumerable<CountryChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class CountryChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string Abbreviation { get; set; }

            [SitecoreField]
            public virtual string SortOrder { get; set; }
        }
        #endregion



        #region "State / Province List"
        public IEnumerable<StateProvinceChildItem> StateProvinceList { get; set; }

        public class StateProvinceItem
        {
            public virtual IEnumerable<StateProvinceChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class StateProvinceChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string Abbreviation { get; set; }

            [SitecoreField]
            public virtual string ParentCountry { get; set; }

            [SitecoreField]
            public virtual string SortOrder { get; set; }
        }
        #endregion



        #region "Industry List"
        public IEnumerable<IndustryChildItem> IndustryList { get; set; }
        public string IndustryChildMenu { get; set; }


        public class IndustryItem
        {
            public virtual IEnumerable<IndustryChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class IndustryChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string SortOrder { get; set; }
        }
        #endregion



        #region "Sub-industry List"
        public IEnumerable<SubIndustryChildItem> SubIndustryList { get; set; }

        public class SubIndustryItem
        {
            public virtual IEnumerable<SubIndustryChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class SubIndustryChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string ParentIndustry { get; set; }

            [SitecoreField]
            public virtual string SortOrder { get; set; }
        }
        #endregion



        #region "Revenue List"
        public IEnumerable<RevenueChildItem> RevenueList { get; set; }

        public class RevenueItem
        {
            public virtual IEnumerable<RevenueChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class RevenueChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string SortOrder { get; set; }
        }
        #endregion



        public void Initialize(Rendering rendering)
        {
            Rendering = rendering;
            Item = rendering.Item;
            PageItem = PageContext.Current.Item;
        }
    }
}

我的观点:

using Glass.Mapper.Sc;
using MySite.Analytics;
using MySite.Models.Items;
using Newtonsoft.Json.Linq;
using Sitecore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace MySite.Presentation.Controllers
{
    public class RegistrationController : AsyncController
        {

        Registration registration = new Registration();

        [HttpGet]
        public PartialViewResult Index(string view)
            {
            if (!string.IsNullOrWhiteSpace(view))
            {
                registration.ReturnUrl = view.Trim();
            }
            if (string.IsNullOrEmpty(registration.ReturnUrl) && Request.UrlReferrer != null)
            {
                registration.ReturnUrl = Server.UrlDecode(Request.UrlReferrer.AbsoluteUri ?? string.Empty);
            }
            PopulateLists();
            return PartialView(registration);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string RegistrationButton, FormCollection formCollection)
        {
            if (!string.IsNullOrEmpty(RegistrationButton))
            {
                try
                {
                    registration.FirstName = formCollection["FirstName"].Trim();
                    registration.LastName = formCollection["LastName"].Trim();
                    registration.Email = formCollection["Email"].Trim();
                    registration.JobTitle = formCollection["JobTitle"].Trim();
                    registration.Company = formCollection["Company"].Trim();
                    registration.Industry = formCollection["Industry"].Trim();
                    registration.SubIndustry = formCollection["SubIndustry"].Trim();
                    registration.RevenueRange = formCollection["RevenueRange"].Trim();
                    registration.Country = formCollection["Country"].Trim();
                    registration.StateProvince = formCollection["StateProvince"].Trim();
                    registration.MarketoTracking = formCollection["_mkt_trk"].Trim();
                    ContactManagement contactManagement = new ContactManagement();
                    contactManagement.RegisterContact(registration);
                }
                catch (Exception ex)
                {
                    Log.Error("MySite.Presentation.Controllers.RegistrationController.Index", ex, this);
                }
            }
            return PartialView();
        }


        public void PopulateLists()
        {
            ISitecoreContext sitecoreContext = new SitecoreContext();
            ISitecoreService service = new SitecoreService(sitecoreContext.Database);

            Guid countryItemID = new Guid("{2FB5AE2D-E80D-4A3D-9201-3E62E781EFCD}");
            Registration.CountryItem countryItem = service.GetItem<Registration.CountryItem>(countryItemID);
            registration.CountryList = countryItem.Children.OrderBy(l => System.Convert.ToInt32(l.SortOrder)).ThenBy(l => l.DisplayName);

            Guid stateProvinceItemID = new Guid("{6171F149-0200-4010-BF5F-5F32EAAE31B4}");
            Registration.StateProvinceItem stateProvinceItem = service.GetItem<Registration.StateProvinceItem>(stateProvinceItemID);
            registration.StateProvinceList = stateProvinceItem.Children.OrderBy(l => System.Convert.ToInt32(l.SortOrder)).ThenBy(l => l.DisplayName);

            Guid industryItemID = new Guid("{ACD77146-0530-4EEE-A477-BF8CD54B9FFE}");
            Registration.IndustryItem industryItem = service.GetItem<Registration.IndustryItem>(industryItemID);
            registration.IndustryList = industryItem.Children.OrderBy(l => System.Convert.ToInt32(l.SortOrder)).ThenBy(l => l.DisplayName);

            Guid subIndustryItemID = new Guid("{43AAABDF-9591-4F03-9894-0EAEFD1292B3}");
            Registration.SubIndustryItem subIndustryItem = service.GetItem<Registration.SubIndustryItem>(subIndustryItemID);
            registration.SubIndustryList = subIndustryItem.Children.OrderBy(l => System.Convert.ToInt32(l.SortOrder)).ThenBy(l => l.DisplayName);

            Guid revenueItemID = new Guid("{86C0FF05-5A9C-4113-BEE6-B68E19BAAB37}");
            Registration.RevenueItem revenueItem = service.GetItem<Registration.RevenueItem>(revenueItemID);
            registration.RevenueList = revenueItem.Children.OrderBy(l => System.Convert.ToInt32(l.SortOrder)).ThenBy(l => l.DisplayName);
        }


        public string GetChildList(string countryDisplayName, IEnumerable<Registration.StateProvinceChildItem> stateProvinceChildItem)
        {
            string menuString = string.Empty;
            if (stateProvinceChildItem != null)
            {
                JArray menuArray = new JArray();
                foreach (var state in stateProvinceChildItem)
                {
                    if (state.ParentCountry == countryDisplayName)
                    {
                        JValue menuItem = new JValue("{\"name\": \"" + state.DisplayName + " (" + state.Abbreviation + ")\", \"value\": \"" + state.Abbreviation + "\"}");
                        menuArray.Add(menuItem);
                    }
                }
                if (menuArray.Count > 0)
                {
                    menuString = "data-child-menu='{\"items\": " + menuArray.ToString() + "}'";
                }
            }
            return menuString;
        }


        public string GetChildList(string industryId, IEnumerable<Registration.SubIndustryChildItem> subIndustryChildItem)
        {
            string menuString = string.Empty;
            if (subIndustryChildItem != null)
            {
                JArray menuArray = new JArray();
                foreach (var subIndustry in subIndustryChildItem)
                {
                    if (subIndustry.ParentIndustry.Trim().ToUpperInvariant().Contains(industryId.Trim().ToUpperInvariant()))
                    {
                        JValue menuItem = new JValue("{\"name\": \"" + subIndustry.DisplayName + "\", \"value\": \"" + subIndustry.DisplayName + "\"}");
                        menuArray.Add(menuItem);
                    }
                }
                if (menuArray.Count > 0)
                {
                    menuString = @"data-child-menu='{""items"": " + menuArray.ToString() + "}'";
                }
            }
            return menuString;
        }

    }
}

1 个答案:

答案 0 :(得分:2)

Sitecore MVC目前存在问题,并且有多种形式。我认为问题在于您将[HttpGet]设置为您的索引操作,这可能会破坏Sitecore路由。您可以在此blog post中检查多个表单的更多问题。