我有一个强类型的ViewModel,如下所示:
public class ShippingRulesViewModel
{
//Stores a reference to the rule to be edited.
public int Editing { get; set; }
public List<ShippingRuleViewModel> ShippingRules { get; set; }
public SelectList ProductFormats { get; set; }
public SelectList ShippingMethods { get; set; }
}
在视图中,我可以迭代ShippingRules并显示每个,如下所示:
@foreach (var _shippingRule in Model.ShippingRules)
{
<tr>
@if (_shippingRule.ID == Model.Editing) //Show rule in edit mode
{
using (Html.BeginForm("EditShippingRule", "Settings", FormMethod.Post))
{
<td>
<input type="submit" class="btn btn-success col-sm-12" value="Save"/>
</td>
<td>@Html.Raw(_shippingRule.ID)</td>
<td>@Html.EditorFor(sr => _shippingRule.ShipServiceLevel, new { htmlAttributes = new { @class = "form-control input-sm mb-md" } })</td>
... etc.
现在我希望能够将单个ShippingRuleViewModel传递回控制器,以便我可以保存编辑。这可以在没有传递整个ShippingRulesViewModel ViewModel的情况下完成吗? (注意名称差异)。
我试过这样做:
[System.Web.Mvc.HttpPost]
public ActionResult EditShippingRule([FromBody]ShippingRuleViewModel shippingRuleViewModel)
{
......但我没有得到任何回报 - 一切都是空的。
我做错了什么,或者我无法实现我想要的目标?