gpt4 book ai didi

c# - RouteValueDictionary 值在 POST 上从字符串更改为数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:49 26 4
gpt4 key购买 nike

我有一个 RouteValueDictionary 附加到传递给 View 的对象。该对象包含一个名为 SelectedItems 的其他对象数组,用于用用户可以选择的复选框填充网格。由于此对象随后会传回 Controller 并根据用户与 UI 的交互方式进行不同的路由,因此它还包含一个名为 ReturnValues 的 RouteValueDictionary。该类的基本设置大致如下所示:

[Serializable]
public class ItemSelection
{
public object[] SelectedItems { get; set; }
public RouteValueDictionary ReturnValues { get; set; }

public ItemSelection()
{
ReturnValues = new RouteValueDictionary()
{
{ "key1", "routeValue1" },
{ "key2", "routeValue2" },
{ "key3", "routeValue3" }
}
}
}

这本来可以完美地工作,除了 ReturnValues 属性在 POST 中返回“null”

看完this post关于字典的映射,我在 View 上提出了以下解决方案:

<%
foreach(var key in Model.Keys)
{
Html.Render(Html.HiddenFor(m => m[key]));
}
%>

这为 html 呈现了一堆隐藏的输入,每个项目看起来像这样:

<input id="ReturnValues__key1_" name="ReturnValues.[key1]" type="hidden" value="routeValue1">

我也尝试过类似于 this post 的东西:

<%
for (var i = 0; i < Model.Count; i++)
{
Html.Hidden("ReturnValues[" + i + "].Key", Model.ElementAt(i).Key);
Html.Hidden("ReturnValues[" + i + "].Value", Model.ElementAt(i).Value);
}
%>

呈现这个:

<input id="ReturnValues_ReturnValues_0__Key" name="ReturnValues.ReturnValues[0].Key" type="hidden" value="key1">
<input id="ReturnValues_ReturnValues_0__Value" name="ReturnValues.ReturnValues[0].Value" type="hidden" value="routeValue1">

这两者都有些作用,除了当 RouteValueDictionary 被发送回 Controller 时,Values 属性不再是 StringsString[1] 的集合(即,字符串变成数组)。因此,抽出后,字典条目看起来像这样:

{ "key1", "routeValue1" }

对此:

{ "key1", { "routeValue1" } }

我已经研究了好几个小时了,但我似乎无法找出问题所在。由于我的 Controller 中的代码正在路由到关联值的 ToString(),因此我的完整路由最终看起来像 /area/controller/System.String[]/

在将我的 RouteValueDictionary 映射到隐藏字段时,我是否遗漏了什么?

更新:我最终为 RouteValueDictionary 对象制作了一个自定义模型 Binder 。 (见下面的答案)

最佳答案

我最终为 RouteValueDictionary 对象制作了一个自定义模型联编程序。以下是相关方法:

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
RouteValueDictionary baseModel = (bindingContext.Model as RouteValueDictionary) ?? (base.BindModel(controllerContext, bindingContext) as RouteValueDictionary);

if (baseModel != null)
{ // start off with a direct copy of the bound model
var returnModel = new RouteValueDictionary();
baseModel.ForEach(x => returnModel[x.Key] = item.Value);

// Sometimes the DefaultModelBinder turns the RouteValueDictionary.Values property into a collection of single-item arrays. This resets them to the correct values.
foreach (var key in baseModel.Keys)
{
returnModel[key] = GetValue(bindingContext, key); // GetValue method body below
}

return returnModel;
}
}

return null;
}

private object GetValue(ModelBindingContext context, string key)
{
var name = String.Format("{0}[{1}]", String.IsNullOrEmpty(context.ModelName) ? String.Empty : context.ModelName, key);
var attemptedResult = context.ValueProvider.GetValue(name);

if (attemptedResult != null && attemptedResult.AttemptedValue != null)
{
return attemptedResult.AttemptedValue;
}

return null;
}

关于c# - RouteValueDictionary 值在 POST 上从字符串更改为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15387132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com