简体   繁体   English

ASP.NET MVC 5 TryUpdateModel不允许&在字符串中

[英]ASP.NET MVC 5 TryUpdateModel not allowing & in string

Starting with an original string of: "Text=AB&C", this value is passed through the HttpUtility.ParseQueryString to create a NameValueCollection. 从原始字符串“ Text = AB&C”开始,此值通过HttpUtility.ParseQueryString传递,以创建NameValueCollection。

Then to create the IValueProvider for the TryUpdateModel, the NameValueCollection creates a new NameValueCollectionProdvier. 然后,为TryUpdateModel创建IValueProvider,NameValueCollection创建一个新的NameValueCollectionProdvier。

At this point in the NameValueCollectionValueProvider, the Key becomes Text and the Value still remains AB&C. 此时,在NameValueCollectionValueProvider中,键变为Text,并且值仍保持为AB&C。

When the TryUpdateModel is called passing in the values, the Text changes from AB&C to AB. 当调用TryUpdateModel传递值时,文本从AB&C更改为AB。

Any thoughts on why this might be? 对为什么会这样有任何想法吗?

Here is the code: 这是代码:

e is from a foreach that was passed in to the Controller ActionResult model value e来自传递给Controller 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);

The model's Text value is declared as public string Text { get; set; } 模型的Text值声明为public string Text { get; set; } public string Text { get; set; } public string Text { get; set; } so the TryUpdateModel does find the matching Text declaration. public string Text { get; set; }所以TryUpdateModel确实找到匹配的文字声明。

Updated: 更新:

I found that within the NameValueCollectionValueProvider, the provider's Value has an _unvalidatedCollection and a _validatedCollection with my expected result of Text=AB&C but the UnvalidatedResult and the ValidatedResult within the provider both have a value of AB. 我发现在NameValueCollectionValueProvider中,提供程序的Value具有_unvalidatedCollection和_validatedCollection,其预期结果为Text = AB&C,但是提供程序中的UnvalidatedResult和ValidatedResult的值均为AB。

It seems that the provider is removing values after the &. 似乎提供程序正在删除&之后的值。

Can the provider itself be further encoded to maintain the & in the string? 提供程序本身是否可以进一步编码以维护字符串中的&?

你需要,当你正在构建的URL编码和解码,当你试图读取网址参数string queryString = "Text=" + HttpUtility.UrlEncode("AB&C") ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM