डिफ़ॉल्ट एमवीसी मॉडल बाइंडर cannot parse value formatted for display। आह
ModelBinders.Binders.Add(typeof(Foo), new FooModelBinder());
, और पिछले: तो, आप अपने खुद के मॉडल बांधने की मशीन लिख सकते हैं और इस प्रकार के लिए यह रजिस्टर करना चाहिए (मान लीजिए प्रकार का नाम फू है):
public class FooModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue("Amount");
if (result != null)
{
decimal amount;
if (Decimal.TryParse(result.AttemptedValue, NumberStyles.Currency, null, out amount))
return new Foo { Amount = amount };
bindingContext.ModelState.AddModelError("Amount", "Wrong amount format");
}
return base.BindModel(controllerContext, bindingContext);
}
}
Application_Start पर फू प्रकार के लिए इस बांधने की मशीन जोड़े बात - राशि पाठ बॉक्स से data-val-number
विशेषता को दूर (अन्यथा आप देख संदेश जारी रहेगा कि यह एक संख्या नहीं है):
$("#Amount").removeAttr("data-val-number");
अब आप सत्यापन त्रुटि messag मिल जाएगा ई अगर इनपुट मूल्य सही मुद्रा राशि नहीं होगी (उदा। $10F.0
)।
BTW मुझे लगता है कि यह ApplyFormatInEditMode = false
उपयोग करने के लिए की तुलना में यह सब सामान को लागू करने में मदद करने MVC अपने कस्टम स्वरूपित स्ट्रिंग बाँध बेहतर है।
स्रोत
2013-01-22 21:37:28
यह निर्भर करता है कि वर्तमान संस्कृति कौन है –