इस प्रकार मैं इस मुद्दे से संपर्क करना शुरू कर दूंगा। एक कस्टम मॉडल बाइंडर FormKey प्रॉपर्टी के आधार पर निर्माण करना बहुत आसान होगा (जिसे इंडेक्स और/या लेबल द्वारा निर्धारित किया जा सकता है)।
public class CustomFormModel
{
public string FormId { get; set; }
public string Label { get; set; }
public CustomFieldModel[] Fields { get; set; }
}
public class CustomFieldModel
{
public DataType DateType { get; set; } // System.ComponentModel.DataAnnotations
public string FormKey { get; set; }
public string Label { get; set; }
public object Value { get; set; }
}
public class CustomFieldModel<T> : CustomFieldModel
{
public new T Value { get; set; }
}
इसके अलावा, मैंने देखा कि नीचे दी गई टिप्पणियों में से एक फ़िल्टर फ़िल्टर मॉडल था। ऑटोमैपर से जिमी बोगर्ड ने इस विधि के बारे में http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/03/17/a-better-model-binder.aspx पर वास्तव में सहायक पोस्ट किया, और बाद में http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/11/19/a-better-model-binder-addendum.aspx में संशोधित किया। कस्टम मॉडल बाइंडर्स बनाने में मेरे लिए यह बहुत उपयोगी रहा है।
अद्यतन
मुझे एहसास हुआ कि मैं सवाल गलत व्याख्या की, और कहा कि वह विशेष रूप से कैसे "इनपुट फ़ील्ड विभिन्न डेटा प्रकार का प्रतिनिधित्व के परिवर्तनशील साथ" फ़ॉर्म की पोस्टिंग को संभालने के लिए पूछ रहा था। मुझे लगता है कि ऐसा करने का सबसे अच्छा तरीका ऊपर की तरह एक संरचना का उपयोग करना है, लेकिन Composite Pattern का लाभ उठाना है। असल में, आपको IFormComponent
जैसे इंटरफ़ेस बनाने की आवश्यकता होगी और प्रत्येक डेटाटाइप के लिए इसे कार्यान्वित किया जाएगा जिसका प्रतिनिधित्व किया जाएगा। मैंने लिखा और एक उदाहरण इंटरफेस टिप्पणी की व्याख्या कर यह कैसे पूरा किया जाएगा मदद करने के लिए:
public interface IFormComponent
{
// the id on the html form field. In the case of a composite Id, that doesn't have a corresponding
// field you should still use something consistent, since it will be helpful for model binding
// (For example, a CompositeDateField appearing as the third field in the form should have an id
// something like "frmId_3_date" and its child fields would be "frmId_3_date_day", "frmId_3_date_month",
// and "frmId_3_date_year".
string FieldId { get; }
// the human readable field label
string Label { get; }
// some functionality may require knowledge of the
// Parent component. For example, a DayField with a value of "30"
// would need to ask its Parent, a CompositeDateField
// for its MonthField's value in order to validate
// that the month is not "February"
IFormComponent Parent { get; }
// Gets any child components or null if the
// component is a leaf component (has no children).
IList<IFormComponent> GetChildren();
// For leaf components, this method should accept the AttemptedValue from the value provider
// during Model Binding, and create the appropriate value.
// For composites, the input should be delimited in someway, and this method should parse the
// string to create the child components.
void BindTo(string value);
// This method should parse the Children or Underlying value to the
// default used by your business models. (e.g. a CompositeDateField would
// return a DateTime. You can get type safety by creating a FormComponent<TValue>
// which would help to avoid issues in binding.
object GetValue();
// This method would render the field to the http response stream.
// This makes it easy to render the forms simply by looping through
// the array. Implementations could extend this for using an injected
// formatting
void Render(TextWriter writer);
}
मैं यह सोचते हैं कि कस्टम रूपों जो एक रूप पैरामीटर के रूप में निहित किया जा सकता है आईडी के कुछ प्रकार के माध्यम से पहुँचा जा सकता है। उस धारणा के साथ, मॉडल बाइंडर और प्रदाता इस तरह कुछ देख सकता है।
public interface IForm : IFormComponent
{
Guid FormId { get; }
void Add(IFormComponent component);
}
public interface IFormRepository
{
IForm GetForm(Guid id);
}
public class CustomFormModelBinder : IModelBinder
{
private readonly IFormRepository _repository;
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult result;
if(bindingContext.ValueProvider.TryGetValue("_customFormId", out result))
{
var form = _repository.GetForm(new Guid(result.AttemptedValue));
var fields = form.GetChildren();
// loop through the fields and bind their values
return form;
}
throw new Exception("Form ID not found.");
}
}
जाहिर है, सब कोड यहाँ सिर्फ बिंदु पार पाने के लिए है, और पूरी की और वास्तविक उपयोग के लिए साफ करने की आवश्यकता होगी। इसके अलावा, अगर यह पूरा हो जाता है तो यह केवल आईफॉर्म इंटरफेस के कार्यान्वयन से जुड़ा होगा, न कि दृढ़ता से टाइप की गई व्यावसायिक वस्तु।(यह एक शब्दकोश में बदलने के लिए एक बड़ा कदम नहीं होगा और कैसल डिक्शनरी एडाप्टर का उपयोग करके दृढ़ता से टाइप की गई प्रॉक्सी का निर्माण करेगा, लेकिन चूंकि आपके उपयोगकर्ता गतिशील रूप से साइट पर फॉर्म बना रहे हैं, इसलिए शायद आपके समाधान में कोई दृढ़ता से टाइप मॉडल नहीं है और यह अप्रासंगिक है)। उम्मीद है कि यह और मदद करता है।
टिप्पणियों के लिए धन्यवाद, बहुत अंतर्दृष्टि। – DanP