2011-04-03 14 views
8

मैं ASP.NET MVC 3 आरटीएम उपयोग कर रहा हूँ निकाल देता है, और मैं इस तरह के एक दृश्य मॉडल है:बाध्यकारी त्रुटि मान उपयोगकर्ता inputted

public class TaskModel 
{ 
    // Lot's of normal properties like int, string, datetime etc. 
    public TimeOfDay TimeOfDay { get; set; } 
} 

TimeOfDay संपत्ति एक कस्टम struct मैं है है, जो काफी सरल है, इसलिए मैं इसे यहां शामिल नहीं कर रहा हूं। मैंने इस संरचना को बांधने के लिए एक कस्टम मॉडल बाइंडर बनाया है। मॉडल बांधने की मशीन बहुत सरल है:

public class TimeOfDayModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 
     try 
     { 
      // Let the TimeOfDay struct take care of the conversion from string. 
      return new TimeOfDay(result.AttemptedValue, result.Culture); 
     } 
     catch (ArgumentException) 
     { 
      bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Value is invalid. Examples of valid values: 6:30, 16:00"); 
      return bindingContext.Model; // Also tried: return null, return value.AttemptedValue 
     } 
    } 
} 

मेरे कस्टम मॉडल बांधने की मशीन ठीक काम करता है, लेकिन जब उपयोगकर्ता प्रदान किया गया मान परिवर्तित नहीं किया जा सकता है या पार्स समस्या है। जब ऐसा होता है (जब टाइमऑफडे कन्स्ट्रक्टर ArgumentException फेंकता है), तो मैं एक मॉडल त्रुटि जोड़ता हूं जो दृश्य में सही ढंग से प्रदर्शित होता है, लेकिन उपयोगकर्ता द्वारा टाइप किया गया मान, जिसे परिवर्तित नहीं किया जा सकता है, खो जाता है। जिस उपयोगकर्ता ने टेक्स्ट टाइप किया है, वह बस खाली है, और HTML स्रोत में मान विशेषता एक खाली स्ट्रिंग पर सेट है: ""।

संपादित करें: मैं सोच रहा हूँ अगर यह जो कुछ गलत कर रहा है अपने संपादक टेम्पलेट हो सकता है, तो मैं इसे यहाँ शामिल कर रहा हूँ:

@model Nullable<TimeOfDay> 
@if (Model.HasValue) 
{ 
    @Html.TextBox(string.Empty, Model.Value.ToString()); 
} 
else 
{ 
    @Html.TextBox(string.Empty); 
} 

मुझे यकीन है कि मूल्य नहीं खोया है कैसे कर सकता हूँ जब बाध्यकारी त्रुटि होती है, तो उपयोगकर्ता मान को सही कर सकता है?

उत्तर

17

आह! मुझे अंत में जवाब मिला! This blog post ने जवाब दिया। मैं जो खो रहा था वह मेरे मॉडल बाइंडर में ModelState.SetModelValue() पर कॉल करना है। तो कोड इस तरह होगा:

public class TimeOfDayModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 
     try 
     { 
      // Let the TimeOfDay struct take care of the conversion from string. 
      return new TimeOfDay(result.AttemptedValue, result.Culture); 
     } 
     catch (ArgumentException) 
     { 
      bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Value is invalid. Examples of valid values: 6:30, 16:00"); 
      // This line is what makes the difference: 
      bindingContext.ModelState.SetModelValue(bindingContext.ModelName, result); 
      return bindingContext.Model; 
     } 
    } 
} 

मुझे उम्मीद है कि यह किसी और को निराशा के घंटों से बचाता है।

संबंधित मुद्दे