2017-08-20 61 views
1

我有許多人已經有過的相同問題 - 模型綁定器不接受本地化的小數輸入。在所有主題,這裏和其他論壇中,推薦的解決方案是實施自定義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; } 
+1

這裏解釋瞭如何以更新的方式執行自定義模型聯編程序。 https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding。如果你有問題,我會爲你建立,但首先嚐試。 – PedroSouki

+0

謝謝,我會試試看,並讓你知道:)我欣賞它 – dzenesiz

+0

@PedroSouki我在看你發表你的評論的文章,​​但我是新來的MVC使用它在我的情況。如果你願意告訴我如何實現它,所以十進制值可以接受「1,25」和「1.25」,我將非常感激。 – dzenesiz

回答

1

看來最終你要實現的目標是屬性級別的結合,如本?:

[PropertyBinder(typeof(PropertyBBinder))] 
public IList<int> PropertyB {get; set;} 

如果這是正確的,這篇文章提供了一個解決方案:Custom model binder for a property

謝謝。

+0

實際上,這是一個十進制屬性,我將在問題中加入。我不知道這是否適用。謝謝你的回答,無論如何:) – dzenesiz

1

在ASP.NET 1.1的核心(Microsoft.AspNetCore.Mvc.Core.Abstraction,1.0.1.0),你會看到如下界面

using System.Threading.Tasks; 

namespace Microsoft.AspNetCore.Mvc.ModelBinding 
{ 
    // 
    // Summary: 
    //  Defines an interface for model binders. 
    public interface IModelBinder 
    { 
     // 
     // Summary: 
     //  Attempts to bind a model. 
     // 
     // Parameters: 
     // bindingContext: 
     //  The Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext. 
     // 
     // Returns: 
     //  A System.Threading.Tasks.Task which will complete when the model binding process 
     //  completes. 
     //  If model binding was successful, the Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.Result 
     //  should have Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingResult.IsModelSet 
     //  set to true. 
     //  A model binder that completes successfully should set Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.Result 
     //  to a value returned from Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingResult.Success(System.Object). 
     Task BindModelAsync(ModelBindingContext bindingContext); 
    } 
} 

型粘合劑在Microsoft.AspNetCore.Mvc.ModelBinding.Binders命名空間,裝配Microsoft.AspNetCore.Mvc.Core, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60定義。他們似乎都沒有公開BindModel()方法,甚至沒有一個虛擬方法。它看起來像你試圖覆蓋一個不存在的方法。

更好的方法是採取現有的ModelBinder,一個最適合您的需求,從它繼承,並覆蓋ModelBindAsync()

+0

謝謝你的迴應。我可以通過問我在哪裏可以找到現有的ModelBinder在我的項目中? – dzenesiz

+1

「模型聯編程序在Microsoft.AspNetCore.Mvc.ModelBinding.Binders名稱空間中定義,程序集Microsoft.AspNetCore.Mvc.Core,Version = 1.0.1.0,Culture = neutral,PublicKeyToken = adb9793829ddae60」。嚴格地說,這意味着你的項目中沒有MB。但是你可以定義一個並將其放在任何你需要的地方,比如在Helpers文件夾或類似的東西里。只需從DefaultModelBinder繼承並重寫BindModelAsync即可。或者乾脆,將ModelBind重命名爲帶有簽名的ModelBindAsync。 –