1. 程式人生 > >ASP.NET MVC 下自定義模型繫結,去除字串型別前後的空格

ASP.NET MVC 下自定義模型繫結,去除字串型別前後的空格

直接貼程式碼了:

SkyModelBinder.cs

using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;

namespace MvcSample.Extensions
{
    public class SkyModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            
var model = base.BindModel(controllerContext, bindingContext); if (model is BaseSkyViewModel) { ((BaseSkyViewModel)model).BindModel(controllerContext, bindingContext); } return model; } protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor,
object value) { //check if data type of value is System.String if (propertyDescriptor.PropertyType == typeof(string)) { //developers can mark properties to be excluded from trimming with [NoTrim] attribute if (propertyDescriptor.Attributes.Cast<object
>().All(a => a.GetType() != typeof (NoTrimAttribute))) { var stringValue = (string)value; value = string.IsNullOrEmpty(stringValue) ? stringValue : stringValue.Trim(); } } base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); } } }

 

BaseSkyViewModel.cs

using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcSample.Extensions
{
    /// <summary>
    /// Base sky view model
    /// </summary>
    [ModelBinder(typeof(SkyModelBinder))]
    public partial class BaseSkyViewModel
    {
        public BaseSkyViewModel()
        {
            this.CustomProperties = new Dictionary<string, object>();
            PostInitialize();
        }

        public virtual void BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
        }

        /// <summary>
        /// Developers can override this method in custom partial classes
        /// in order to add some custom initialization code to constructors
        /// </summary>
        protected virtual void PostInitialize()
        {
            
        }

        /// <summary>
        /// Use this property to store any custom value for your models. 
        /// </summary>
        public Dictionary<string, object> CustomProperties { get; set; }
    }

    /// <summary>
    /// Base Sky entity model
    /// </summary>
    public partial class BaseSkyEntityViewModel : BaseSkyViewModel
    {
        public virtual int Id { get; set; }
    }
}

 

NoTrimAttribute.cs

using System;

namespace MvcSample.Extensions
{
    /// <summary>
    /// Attribute indicating that entered values should not be trimmed
    /// </summary>
    public class NoTrimAttribute : Attribute
    {
    }
}

 

DEMO

ProductInfoModifyViewModel.cs

using MvcSample.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcSample.Models
{
    public class ProductInfoModifyViewModel : BaseSkyViewModel
    {
        public int ProductId { get; set; }

        /// <summary>
        /// 假設有一個這個 Property
        /// </summary>
        public string ProductName { get; set; }

        public int NewYear { get; set; }
    }
}

HomeController.cs

using MvcSample.Extensions;
using MvcSample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcSample.Controllers
{
    public class HomeController : Controller
    {
        [HttpPost]
        public ActionResult ModifyProduct(ProductInfoModifyViewModel viewModel,
            [ModelBinder(typeof(CommaSeparatedModelBinder))] List<int> orderStatusIds = null)
        {
            ProductInfoService.TryModifyById(viewModel.ProductId, viewModel.NewYear);
            return Json(new { Success = true, Message = "儲存成功" });
        }
    }
}

 

執行截圖:

 

圖2:

 

謝謝瀏覽!