我有許多人已經有過的相同問題 - 模型綁定器不接受本地化的小數輸入。在所有主題,這裏和其他論壇中,推薦的解決方案是實施自定義ModelBinder
。實現自定義ModelBinder ASP.NET MVC
我的問題是,這些解決方案不知何故不適合我。讓我們用這個解決方案,例如:comma decimal seperator in asp.net mvc 5
當我引用的所有命名空間,兩個錯誤仍然存在:
錯誤CS0115 'DecimalModelBinder.BindModel(ControllerContext, ModelBindingContext)':發現沒有覆蓋合適的方法.. 。
和
錯誤條件表達式CS0173型不能被阻止開採 因爲有「布爾」和「小數」
當第二個引用整個return
聲明之間不存在隱式轉換。
在MVC框架中做了些改變,所以這段代碼已經過時了,還是我做錯了什麼?
我結束了與該代碼是:有問題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.Mvc;
namespace AetMuzickaOprema.App_Start
{
public class DecimalModelBinder : System.Web.ModelBinding.DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, System.Web.ModelBinding.ModelBindingContext bindingContext) //first error
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue); //second error
}
}
}
模型屬性:
[Required]
[Range(typeof(decimal), "0", "999999999")]
public decimal Price { get; set; }
這裏解釋瞭如何以更新的方式執行自定義模型聯編程序。 https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding。如果你有問題,我會爲你建立,但首先嚐試。 – PedroSouki
謝謝,我會試試看,並讓你知道:)我欣賞它 – dzenesiz
@PedroSouki我在看你發表你的評論的文章,但我是新來的MVC使用它在我的情況。如果你願意告訴我如何實現它,所以十進制值可以接受「1,25」和「1.25」,我將非常感激。 – dzenesiz