【问题标题】:MVC Save Page Model's List of Custom Items on Submit to use in Post MethodMVC 在提交时保存页面模型的自定义项目列表以在 Post 方法中使用
【发布时间】:2020-12-14 11:35:09
【问题描述】:

我有一个名为 DocumentDTO 的自定义对象列表,它是在页面加载时创建并填充值的。提交页面时,我需要提交列表中的值以在控制器 post 方法中使用。

有没有办法为带有自定义对象的列表项做到这一点?当它是一个字符串列表时,我能够做到这一点,但是当它的自定义对象时它不起作用

对于其他领域我这样做:

 < input asp-for="FileID" type="hidden" /> 

对于字符串列表,我这样做:

    @for (int i = 0; i < Model.StringList.Count(); i++)
    {
        <input asp-for="StringList[i]" type="text">
    }

但我有这个模型:

public class FileModel
{
     public string Email { get; set; }

     public string FileNumber { get; set; }

     public List<DocumentDTO> DocumentListing { get; set; }

    public class DocumentDTO
    {
    
        public string DocName { get; set; }

        public string Comments { get; set; }

        public int docID { get; set; }

    }
}

但是下面的两种尝试都不允许我在提交时看到控制器的 Post 方法中的列表项(列表说它的计数为 0)

int DocumentsCount = Model.DocumentListing.Count();


 for (int i = 0; i < DocumentsCount; i++)
{
    <input asp-for="DocumentListing[i].DocName" type="hidden">
    <input asp-for="DocumentListing[i].docID" type="hidden">
    <input asp-for="DocumentListing[i].Comments" type="hidden">
}

这可能吗?如果可能的话,有人可以帮忙吗?

【问题讨论】:

  • 您好@user9758771,请务必删除剃刀视图中的无效输入。也就是说,删除&lt;input asp-for"DocumentListing" type="hidden"&gt;&lt;input asp-for="DocumentListing[i]" type="hidden"&gt;。然后您可以成功将数据发布到后端。

标签: c# asp.net-core-mvc


【解决方案1】:

首先,您需要知道对于复杂类型的每个属性,模型绑定会在源中查找名称模式prefix.property_name。如果未找到任何内容,则仅查找不带前缀的 property_name

在您的代码中,您的剃刀视图中有DocumentListingDocumentListing[i]DocumentListing[i].DocName。但是DocumentListingDocumentListing[i] 无效,模型绑定无法为它们查找相应的属性。

确保删除无效输入并将有效属性发布到后端:

@model FileModel
@{int DocumentsCount = Model.DocumentListing.Count(); }
<form asp-action="Create">
    @*<input asp-for="DocumentListing" type="hidden">*@          //remove this

    @for (int i = 0; i < DocumentsCount; i++)
    {
        Model.DocumentListing.Add(Model.DocumentListing.ElementAtOrDefault(i));

        @*<input asp-for="DocumentListing[i]" type="hidden">*@    //remove this
        <input asp-for="DocumentListing[i].DocName" type="hidden">
    }
    <input type="submit" value="create" />
</form>

更新:

控制器:

[HttpPost]
public IActionResult Create([Bind(Prefix = "DocumentListing")]List<DocumentDTO> model)
{
    //...
    return View();
}

或者:

[HttpPost]
public IActionResult Create(FileModel model)
{
    //...
    return View();
}

在前端检查您的 html 应该如下所示:

更新2:

【讨论】:

  • 好吧,很高兴知道,我刚刚把它们拿出来了。有趣的是,通过 post 传递的模型仍然没有任何列表项。是否可以通过列表项?而且我注意到那些隐藏字段最终通过表单本身传递,而不是通过模型。我不知道为什么会这样。
  • 嗨@user9758771,你能更新你的剃刀视图吗?否则我无法重现这个问题。谢谢。
  • 当然,我刚刚更新了它。理想情况下,我只想将列表中的每个完整 DocumentListing 对象传递给发布请求。谢谢
  • 有点难以置信,我使用了你的代码,仍然可以成功地将数据传递到后端。确保你的后端操作包含[HttpPost],reveive 模型应该是FileModel。我认为你需要检查在浏览器中输入名称。输入名称应为:name="DocumentListing[0].DocName"只需按 F12 并检查如下 gif:i.stack.imgur.com/1PkkR.gif
  • 如果你的后端接收到的模型不是FileModel,你也可以使用[Bind(Prefix = "DocumentListing")]List&lt;DocumentDTO&gt; model
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 2019-08-09
  • 1970-01-01
  • 2019-10-18
  • 2017-03-16
  • 2023-03-15
  • 2019-01-23
相关资源
最近更新 更多