【问题标题】:asp.net, razor: when model is returned it will construct it anewasp.net, razor: 当返回模型时,它会重新构造它
【发布时间】:2022-11-18 22:41:07
【问题描述】:

我像这样夸大我的观点

public ActionResult Tagging(int id, ItemType itemType, bool autoCloseWindow = false, bool refreshOpener = false)
        {
            var model = new TaggingViewModel(id, itemType);

            return View("Tagging", model);
        }

我给参数 id 和 itemtype 来检索正确的数据。

我像这样(正确地)显示数据:

<td>
    @Html.CheckBoxFor(m => m.MainNodes[i].children[y].IsChecked, new { @class = "langCheck" })
    @Html.HiddenFor(m => m.MainNodes[i].children[y].ItemId)
    @Html.HiddenFor(m => m.MainNodes[i].children[y].GlobalTaggingId)
    @Html.HiddenFor(m => m.MainNodes[i].children[y].ItemType)
</td>

但是,如果我在我的表单字段中按下保存,模型将返回为空:

我发现原因是在 saveTagging 方法的参数中我重新启动了模型,但是没有参数(因为你不能在这里传递它们)因此返回了一个空模型。

但:

1.) 我如何给它参数?

2.) 即使我静态地给参数说,我也不想返回旧模型,我希望返回前端修改后的模型。

【问题讨论】:

    标签: asp.net razor


    【解决方案1】:

    事实证明,我几乎做对了所有事情。

    您不能简单地返回带有 ID 的整个模型,但您可以简单地从您需要的模型中返回属性。意思是我更改了我的操作结果中的参数:

    public ActionResult SaveTagging(TaggingViewModel taggedModel, bool autoCloseWindow = false)
    

    至:

    public ActionResult SaveTagging(List<NodeModelWithCheckBoxes> MainNodes, bool autoCloseWindow = false)
    

    List<NodeModelWithCheckBoxes> MainNodes
    

    是具有返回 null 的值的列表。这个列表 ofc 是我模型的一部分 taggedModel 这样,它就不会重新实例化,我所有的值都会传递到这个对象中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多