ASP.NET MVC 5 TryUpdateModel不允许&;字符串中
本文关键字:amp 字符串 不允许 NET MVC TryUpdateModel ASP | 更新日期: 2023-09-27 18:27:53
从一个原始字符串"Text=AB&C"开始,该值通过HttpUtility.ParseQueryString来创建NameValueCollection。
然后,为了为TryUpdateModel创建IValueProvider,NameValueCollection创建了一个新的NameValueCollectionProdvier。
此时,在NameValueCollectionValueProvider中,键变为Text,值仍然保持AB&C.
当调用TryUpdateModel传递值时,Text从AB&C至AB.
有什么想法吗?
这是代码:
e来自传递给控制器ActionResult模型值的foreach
foreach (string e in model.Elements)
{
string queryString = HttpUtility.UrlDecode(e);
var model = new Model();
NameValueCollection nameValues = HttpUtility.ParseQueryString(queryString);
// nameValues = {Text=AB&C} at this point
var provider = new NameValueCollectionValueProvider(nameValues, System.Globalization.CultureInfo.CurrentUICulture);
// provider = NameValueCollectionValueProvider at this point
// It's _values has a Count=1
// which at index [0] has this value {[Text, System.Web.Mvc.NameValueCollectionProvider+ValueProviderResultPlaceholder]}
// which has a Key of "Text"
// and a Value of System.Web.Mvc.NameValueCollectionProvider
// within that Value, it's broken down with some properties
// System.Web.Mvc.NameValueCollectionProvider
// _validatedCollection and _unvalidatedCollection both have a value of Text=AB&C
// but I see now that the ValidatedResult and the UnvalidatedResult both have a value of AB
TryUpdateModel(model, provider);
模型的Text值被声明为public string Text { get; set; }
,因此TryUpdateModel确实找到了匹配的Text声明。
更新:
我发现在NameValueCollectionValueProvider中,提供者的Value有一个_unvalidateCollection和一个_validateCollection,我的预期结果是Text=AB&C,但是提供程序中的UnvalidatedResult和ValidatedResult的值都为AB。
提供程序似乎正在删除&之后的值;。
提供者本身是否可以被进一步编码以维护&在绳子上?
构建url时需要编码,读取url参数时需要解码string queryString = "Text=" + HttpUtility.UrlEncode("AB&C")
;