如何在 MVC 中捕获已发布的复杂结构
How to catch posted complex structure in MVC
如何通过表单提交将嵌套数据 post 嵌套到 ActionResult 参数中?
我一直在尝试实施类似于 的答案的解决方案,但运气不佳。
相关表格部分的结构如下:
<form method="post" action="/Product/Edit" class="tabs">
<input name="Prices[1].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[1].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[3].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[3].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[3].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">
<button type="submit">submit</button>
</form>
这在日志中正常显示,posted 数据按预期显示:
Form Submission Event: (BaseController:57-ish):
{
...
"Prices[1].Price": "9.9900",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "5.5800",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",
"Prices[3].Price": "0.0000",
"Prices[3].Size2Price": "0.0000",
"Prices[3].Size3Price": "0.0000",
...
}
在模型中,我一直在尝试变体来尝试捕捉价格的集合,目前的尝试如下:
[DataContract]
public class EditedProductEntry
{
[DataContract]
public struct PriceCollection
{
[DataMember( Name = "Price" )]
public string Price { get; set; }
[DataMember( Name = "Size2Price" )]
public string Size2Price { get; set; }
[DataMember( Name = "Size3Price" )]
public string Size3Price { get; set; }
}
...
[DataMember( Name = "Prices" )]
public PriceCollection[] Prices { get; set; }
...
}
在控制器端,我有 "Edit" ActionResult 接收 "EditedProductEntry" 数据结构:
[HttpPost]
[Route( "Edit", Name = "Product_Edit" )]
public ActionResult Edit( EditedProductEntry post )
{
...
}
我一直在摆弄 "post" 参数的结构,试图让它接受 "Prices" 集合,但这让我感到难过。其余数据通过良好并正在填充 "EditedProductEntry" 对象,但是集合坚决保留空值。
我试过捕获一个对象[]、EditedProductEntry[]、ICollection、IEnumerable 并且还尝试将它们指定为 "Edit" 操作的附加参数,但到目前为止我还没有成功.
还有人有其他想法吗?
我在本地机器上尝试了同样的情况,发现控件名称的索引不正确。
控件名称应遵循从零开始的索引,因此发布的数据也应遵循。
输入控件应声明如下。
<input name="Prices[0].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[0].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[0].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[1].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[1].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">
所以日志文件也会有以下数据。
"Prices[0].Price": "9.9900",
"Prices[0].Size2Price": "0.0000",
"Prices[0].Size3Price": "0.0000",
"Prices[1].Price": "5.5800",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "0.0000",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",
进行此更改应该可以解决您的问题。
如何通过表单提交将嵌套数据 post 嵌套到 ActionResult 参数中?
我一直在尝试实施类似于
相关表格部分的结构如下:
<form method="post" action="/Product/Edit" class="tabs">
<input name="Prices[1].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[1].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[3].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[3].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[3].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">
<button type="submit">submit</button>
</form>
这在日志中正常显示,posted 数据按预期显示:
Form Submission Event: (BaseController:57-ish):
{
...
"Prices[1].Price": "9.9900",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "5.5800",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",
"Prices[3].Price": "0.0000",
"Prices[3].Size2Price": "0.0000",
"Prices[3].Size3Price": "0.0000",
...
}
在模型中,我一直在尝试变体来尝试捕捉价格的集合,目前的尝试如下:
[DataContract]
public class EditedProductEntry
{
[DataContract]
public struct PriceCollection
{
[DataMember( Name = "Price" )]
public string Price { get; set; }
[DataMember( Name = "Size2Price" )]
public string Size2Price { get; set; }
[DataMember( Name = "Size3Price" )]
public string Size3Price { get; set; }
}
...
[DataMember( Name = "Prices" )]
public PriceCollection[] Prices { get; set; }
...
}
在控制器端,我有 "Edit" ActionResult 接收 "EditedProductEntry" 数据结构:
[HttpPost]
[Route( "Edit", Name = "Product_Edit" )]
public ActionResult Edit( EditedProductEntry post )
{
...
}
我一直在摆弄 "post" 参数的结构,试图让它接受 "Prices" 集合,但这让我感到难过。其余数据通过良好并正在填充 "EditedProductEntry" 对象,但是集合坚决保留空值。
我试过捕获一个对象[]、EditedProductEntry[]、ICollection、IEnumerable 并且还尝试将它们指定为 "Edit" 操作的附加参数,但到目前为止我还没有成功.
还有人有其他想法吗?
我在本地机器上尝试了同样的情况,发现控件名称的索引不正确。
控件名称应遵循从零开始的索引,因此发布的数据也应遵循。
输入控件应声明如下。
<input name="Prices[0].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[0].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[0].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[1].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[1].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">
所以日志文件也会有以下数据。
"Prices[0].Price": "9.9900",
"Prices[0].Size2Price": "0.0000",
"Prices[0].Size3Price": "0.0000",
"Prices[1].Price": "5.5800",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "0.0000",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",
进行此更改应该可以解决您的问题。