gpt4 book ai didi

C# MVC 模型在发布时绑定(bind)到索引数组

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

是否可以将模型绑定(bind)到已发布的损坏索引数组?

<input type='text' name='question[3]' value='answer to question 3' />
<input type='text' name='question[5]' value='whatever here' />
<input type='text' name='question[18]' value='a different ansewr' />

<input type='text' name='multiquestions[4][1]' value='question 4 part 1' />
<input type='text' name='multiquestions[4][2]' value='question 4 part 2' />

理想情况下,我希望我的模型符合以下几条线(尽管输入这个我可以看到稀疏数组键是一个问题):

public class MyModel {
public Dictionary<int,string> questions {get;set;}
public Dictionary<int, Dictionary<int, string>> multiquestions {get;set;}
}

让 MVC 模型绑定(bind)器完成繁重的工作:

public ActionResult MyAction(MyModel model) {...}

但它似乎只适用于未损坏的索引。我需要索引,因为这是我将数据放入数据库的方式。

我宁愿没有一系列描述模型的隐藏字段,例如<input type=hidden name='questions[0].Key' value='3' /><input type=hidden name='questions[0].Value' value='answer to question 3' />

我读过 Hanselman blog post关于这个主题,如果开箱即用,建议编写自定义模型 Binder ,但不确定从哪里开始编写自定义 Binder ,我希望 MVC 可以“开箱即用”地做一些事情,或者那个somoene已经解决了这个问题

最佳答案

经过更多的搜索,事实证明不使用 int 作为字典键是诀窍。

使用 Dictionary<string, string> 模型绑定(bind)将按我预期的方式工作和 Dictionary<string, Dictionary<string, string>> ,当我在服务器上时,我需要将 key 解析为 int。

关于C# MVC 模型在发布时绑定(bind)到索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138628/

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