从的FormCollection数据绑定到模型 [英] Data Binding from FormCollection to Model
问题描述
我试图从绑定我的观点获得一个特定的模型数据。我想表单数据转换成字典对象。这是使用我开发的,TranslateFormData一个通用的方法来实现。这是我遇到的问题。
下面是控制器code:
[HttpPost]
公共System.Web.Mvc.ActionResult SubmitDecision(的FormCollection FORMDATA)
{
字典<字符串,字符串>数据= FormData.TranslateFormData(FORMDATA);
TPDispatcherNetClient客户端=新TPDispatcherNetClient(); *额外的code冷落为了简洁* 返回查看();
}
这行,词典<字符串,字符串>数据= FormData.TranslateFormData(FORMDATA);
,导致我的问题。这里是code为TranslateFormData:
公共静态字典<字符串,字符串> TranslateFormData< T> (T型),其中T:IEnumerable的< KeyValuePair<字符串,字符串>>
{
字典<字符串,字符串>数据=新词典与LT;字符串,字符串> ();
IEnumerator的< KeyValuePair<字符串,字符串>>成对= form.GetEnumerator(); 返回的数据;
}
在传递的FormCollection正在产生以下错误:
类型System.Web.Mvc.FormCollection'不能被用作类型参数在通用类型或方法TranslateFormData(T),T。有一个从System.Web.Mvc.FormCollection'到'System.Collections.Generic.IEnumerable>
隐式>引用转换
块引用>我怎样才能做到这一点?我想避免更改TranslateFormData接受一个特定的类型,然后根据所有预期的类型超载,如果在所有可能的。
编辑
由于最初写这个问题我已经相应的修改我的翻译方法:
块引用>公共静态CustomerDecisionRequest TranslateFormData< T> (T型),其中T:NameValueCollection中,IEnumerable的
{
CustomerDecisionRequest要求=新CustomerDecisionRequest();
IEnumerator的枚举= formData.GetEnumerator(); 返回请求;
}因此,虽然上述问题表示,这是否修改实际意义,它是适当的?
解决方案感谢您所有的意见。我采用的方法是基于无知。我能得到我的看法张贴正确的数据,使用正确的模式,回到我的控制器。此外,我不需要定制/特殊装订。
我的问题,没有正确设置我的模型撒谎。我只是在指定的公共领域,而不是自动的公共属性。一旦我做了改变,其他的一切就明白了。
I am trying to bind data received from my view to a specific model. I want to translate the form data into a dictionary object. This is accomplished using a generic method I developed, TranslateFormData. This is where I am having issues.
Here is the controller code:
[HttpPost] public System.Web.Mvc.ActionResult SubmitDecision ( FormCollection formData ) { Dictionary<string, string> data = FormData.TranslateFormData ( formData ); TPDispatcherNetClient client = new TPDispatcherNetClient ( ); *Additional code left out for brevity* return View ( ); }
This line,
Dictionary<string, string> data = FormData.TranslateFormData ( formData );
, is causing me issues. Here is the code for TranslateFormData:public static Dictionary<string, string> TranslateFormData<T> ( T form ) where T : IEnumerable<KeyValuePair<string, string>> { Dictionary<string, string> data = new Dictionary<string, string> ( ); IEnumerator<KeyValuePair<string, string>> pairs = form.GetEnumerator ( ); return data; }
Passing in the FormCollection is producing the following error:
The type 'System.Web.Mvc.FormCollection' cannot be used as type parameter 'T' in the generic type or method 'TranslateFormData(T)'. There is no implicit >reference conversion from 'System.Web.Mvc.FormCollection' to 'System.Collections.Generic.IEnumerable>'
How can I accomplish this? I want avoid changing TranslateFormData to accept a specific type and then overloading based on all expected types if at all possible.
EDIT Since initially writing this question I have modified my translation method accordingly:
public static CustomerDecisionRequest TranslateFormData<T> ( T form ) where T : NameValueCollection, IEnumerable { CustomerDecisionRequest request = new CustomerDecisionRequest ( ); IEnumerator enumerator = formData.GetEnumerator ( ); return request; }
So, while the above questions stands, does this modification make sense and is it appropriate?
解决方案Thank you for all the comments. The approach I was taking was based on ignorance. I was able to get my view to post the correct data, using the correct model, back to my controller. In addition, I need no custom/special binding.
My problem lied in not setting up my model correctly. I was just specifying public fields instead of automatic public properties. Once I made that change, everything else fell into place
这篇关于从的FormCollection数据绑定到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!