1 頁 (共 1 頁)

dropdownlist 資料丟到 ActionFilterAttribute 處理

發表於 : 週四 11月 04, 2010 4:04 pm
rusli
簡單說明: 之後補上

Model

發表於 : 週四 11月 04, 2010 4:05 pm
rusli

代碼: 選擇全部

public class InstallModel
{
        public string StoreAddressOfCity { get; set; }
        public string StoreAddressOfTownship { get; set; }
}

TownDataAttribute

發表於 : 週四 11月 04, 2010 4:06 pm
rusli

代碼: 選擇全部


    using System.Collections;
    using System.Linq;
    using System.Reflection;
    using System.Web.Mvc;
    using Domain;
    using Generic;
    using Globalization;
    using Party.Service;

    public class TownDataAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// The geographic boundary service.
        /// </summary>
        private readonly IGeographicBoundaryService GgeographicBoundaryService = RheaFactory.ServiceFactory.GetService<IGeographicBoundaryService>();

        public string SelectedCityValueFieldName { get; set; }
        public string SelectedTownValueFieldName { get; set; }

        /// <summary>
        /// ViewData 物件名稱初始值
        /// </summary>
        private string viewDataName = "Towns";

        /// <summary>
        /// ViewData 物件名稱
        /// </summary>
        public string ViewDataName
        {
            get { return viewDataName; }
            set { viewDataName = value; }
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string citySelectedValue = string.Empty;
            string townSelectedValue = string.Empty;
            var m = ((ViewResultBase)(filterContext.Result)).ViewData.Model;
            if (m != null )
            {
                citySelectedValue = GetFiledValue(m, SelectedCityValueFieldName);
                townSelectedValue = GetFiledValue(m, SelectedTownValueFieldName);
            }

            IList towns = ((ICity)GgeographicBoundaryService.GetById(citySelectedValue)).Townships;
            IList source = (from t in towns.OfType<ITownship>() select new { t.Id, Name = t.Name.ToI18n("Township") }).ToList();
            filterContext.Controller.ViewData[ViewDataName] = new SelectList(source, "Id", "Name", townSelectedValue);
        }

        private static string GetFiledValue(object model, string fieldName)
        {
            string selectedValue = string.Empty;
            PropertyInfo pI;
            if (!string.IsNullOrEmpty(fieldName))
            {
                pI = (model).GetType().GetProperty(fieldName);
                if (pI != null)
                {
                    selectedValue = (pI.GetValue(model, null) ?? string.Empty).ToString();
                }
            }
            return selectedValue;
        }
    }


CityDataAttribute

發表於 : 週四 11月 04, 2010 4:07 pm
rusli

代碼: 選擇全部

    using System.Collections;
    using System.Linq;
    using System.Web.Mvc;
    using Domain;
    using Globalization;
    using Party.Service;
    using Generic;

    public class CityDataAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// The geographic boundary service.
        /// </summary>
        private readonly IGeographicBoundaryService GeoBoundaryService = RheaFactory.ServiceFactory.GetService<IGeographicBoundaryService>();

        /// <summary>
        /// ViewData 物件名稱初始值
        /// </summary>
        private string viewDataName = "Cities";

        /// <summary>
        /// ViewData 物件名稱
        /// </summary>
        public string ViewDataName
        {
            get { return viewDataName; }
            set { viewDataName = value; }
        }

        public string SelectedCityValueFieldName { get; set; }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            IList cities = GeoBoundaryService.GetObjects(typeof (ICity));
            IList source = (from c in cities.OfType<ICity>() select new {c.Id, Name = c.Name.ToI18n("City")}).ToList();
            string selectedValue = string.Empty;
            var m = ((ViewResultBase) (filterContext.Result)).ViewData.Model;
            if (m != null && !string.IsNullOrEmpty(SelectedCityValueFieldName))
            {
                PropertyInfo pI = (m).GetType().GetProperty(SelectedCityValueFieldName);
                if(pI != null)
                {
                    selectedValue = pI.GetValue(m, null).ToString();
                }
            }
            filterContext.Controller.ViewData[ViewDataName] = new SelectList(source, "Id", "Name", selectedValue);
        }
    }


Controller 使用

發表於 : 週四 11月 04, 2010 4:07 pm
rusli

代碼: 選擇全部


        [CityData(SelectedCityValueFieldName = "StoreAddressOfCity")]
        [TownData(SelectedCityValueFieldName = "StoreAddressOfCity", SelectedTownValueFieldName = "StoreAddressOfTownship")]
        public ActionResult Index()
        {
            return View();
        }


View

發表於 : 週四 11月 04, 2010 4:14 pm
rusli

代碼: 選擇全部


            <%=Html.DropDownListFor(m => m.StoreAddressOfCity, (IEnumerable<SelectListItem>)ViewData["Cities"], "", new { onchange="SetTown(this, '" + Url.Action("GetTown","Install") + "')"})%>
                    <%=Html.DropDownListFor(m => m.StoreAddressOfTownship, (IEnumerable<SelectListItem>)ViewData["Towns"])%>