【问题标题】:Add multiple records of the same model in ASP.NET MVC在 ASP.NET MVC 中添加同一模型的多条记录
【发布时间】:2021-05-20 14:12:11
【问题描述】:

我正在 .NET Framework 中开发一个小型 Web 应用程序,并且在其中一个视图中我需要在表单提交时上传/发送 5 条记录。我在cshtml文件中做了一个for循环,就像这样。

数据模型:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace aspnet_client.Models
{
    /// <summary>
    /// The DataModel class
    /// </summary>
    public class DataModel
    {
        /// <summary>
        /// Gets or sets the identifier.
        /// </summary>
        /// <value>
        /// The identifier.
        /// </value>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        [Required(ErrorMessage = "Se requiere una descripción")]
        [DisplayName("Descripción")]
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the image.
        /// </summary>
        /// <value>
        /// The image.
        /// </value>
        [Required(ErrorMessage = "Se requiere una imagen")]
        [DisplayName("Imagen")]
        public byte[] Image { get; set; }
    }
}

观点:

@model List<aspnet_client.Models.DataModel>
@{
    ViewBag.Title = "AddData";
}

<h2>Añade nuevos registros</h2>
@using (Html.BeginForm(FormMethod.Post))
{
    @Html.ValidationSummary(true, "", new { @class="text-danger"})
        <div class="form-inline">
            @if (Model != null && Model.Count > 0)
            {
                var j = 0;
                foreach (var i in Model)
                {
                    <div class="data-element">
                        <div class="description">
                            @Html.TextBoxFor(x => x[j].Description, new { @class = "form-control", placeholder = "Descripción" })
                            @Html.ValidationMessageFor(model => model[j].Description, "", new { @class="text-danger"})
                        </div>
                        <br />
                        <div>
                            <input type="file" name="ImageData" id="ImageData" onchange="fileCheck(this);" />
                        </div>
                    </div>
                    <br />
                    j++;
                }
            }
     </div>
    <button type="submit" class="btn btn-success">Añadir Registros</button>
}

控制器动作:

using aspnet_client.Models;
using System.Collections.Generic;
using System.Web.Mvc;

namespace aspnet_client.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult AddData()
        {
            var records = new List<DataModel>();

            records.Add(new DataModel());
            records.Add(new DataModel());
            records.Add(new DataModel());
            records.Add(new DataModel());
            records.Add(new DataModel());

            return View(records);
        }

        [HttpPost]
        public ActionResult AddData(DataModel dataModel)
        {
            return View(dataModel);
        }

        [HttpGet]
        public ActionResult GetDataRecords()
        {
            return View();
        }
    }
}

如您在操作代码中所见,我已通过在操作列表中添加 5 个模型来更新我的初始问题,这似乎解决了我的问题(可能是一个更好的解决方案?)

现在的问题是更新输入图像标签中的图像名称属性和ImageId属性。不确定如何执行此操作,因为我无法像上面那样使用@Html 访问模型。文本框中的指令,我卡住了

【问题讨论】:

  • 这篇文章需要更多细节。您可以发布您的数据模型、操作和整体视图吗?
  • 对不起,我已经更新了我的帖子,也许你可以帮忙
  • 谢谢。我想知道为什么您要发送以查看记录列表,但只发回一条记录?
  • 哦,对不起,我的错,只是不完整,还没有到达那里,仍然需要修复它;)一旦我找到了图像的解决方法,这是我现在的主要问题。然后在记录的回发中,我想打开一个 websocket 连接将数据发送到服务器,但那是另一回事
  • 您可能会发现以下有用的 docs.microsoft.com/en-us/aspnet/core/mvc/models/… 它建议使用 IFormFile 接口,而不是在模型中使用字节数组。

标签: c# asp.net-mvc razor


【解决方案1】:

.net 在后台使用控件的 id 和可能的名称来决定如何填充模型。问题是您的页面视图生成的 id 不足以转换为 post 方法所需的模型。

我认为您需要第二个模型来保存 DataModel 列表。

    public class PageModel
    {
      /// <summary>
      /// Gets or sets the list of records.
      /// </summary>
      /// <value>
      /// The identifier.
      /// </value>
      public List<DataModel> Records { get; set; }
    }

将此作为模型显示在您的视图中

@model aspnet_client.Models.PageModel

并改变for循环如下

        @if (Model != null && Model.Records.Count > 0)
        {
            var j = 0;
            foreach (var i in Model.Records)
            {
                <div class="data-element">
                    <div class="description">
                        @Html.TextBoxFor(x => x.Records[j].Description, new { @class = "form-control", placeholder = "Descripción" })
                        @Html.ValidationMessageFor(model => model.Records[j].Description, "", new { @class="text-danger"})
                    </div>
                    <br />
                    <div>
                        @*This will also need changed*@
                        <input type="file" name="ImageData" id="ImageData" onchange="fileCheck(this);" />
                    </div>
                </div>
                <br />
                j++;
            }
        }

发布方式

    [HttpPost]
    public ActionResult AddData(PageModel model)
    {
        //Do stuff with records
    }

获取方法

    [HttpGet]
    public ActionResult AddData()
    {
        var pm = new PageModel();
        pm.Records = new List<DataModel>();

        pm.Records.Add(new DataModel());
        pm.Records.Add(new DataModel());
        pm.Records.Add(new DataModel());
        pm.Records.Add(new DataModel());
        pm.Records.Add(new DataModel());

        return View(pm);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2021-02-26
    相关资源
    最近更新 更多