Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET MVC中添加同一模型的多条记录_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 在ASP.NET MVC中添加同一模型的多条记录

C# 在ASP.NET MVC中添加同一模型的多条记录,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在.NET Framework中开发一个小型web应用程序,在其中一个视图中,我需要上传/发送表单提交中的5条记录。我在cshtml文件中做了一个for循环,如下所示 数据模型: using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace aspnet_client.Models { /// <summary> /// The DataModel clas

我正在.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; }
    }
}
使用System.ComponentModel;
使用System.ComponentModel.DataAnnotations;
命名空间aspnet_client.Models
{
/// 
///数据模型类
/// 
公共类数据模型
{
/// 
///获取或设置标识符。
/// 
/// 
///标识符。
/// 
公共int Id{get;set;}
/// 
///获取或设置描述。
/// 
/// 
///描述。
/// 
[必需(ErrorMessage=“Se require una descriptionón”)]
[显示名称(“描述”)]
公共字符串说明{get;set;}
/// 
///获取或设置图像。
/// 
/// 
///形象。
/// 
[必需(ErrorMessage=“Se require una imagen”)]
[显示名称(“图像”)]
公共字节[]映像{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>
}
@型号列表
@{
ViewBag.Title=“AddData”;
}
新注册会计师协会
@使用(Html.BeginForm(FormMethod.Post))
{
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@if(Model!=null&&Model.Count>0)
{
var j=0;
foreach(模型中的var i)
{
@TextBoxFor(x=>x[j].Description,新建{@class=“form control”,placeholder=“Descripción”})
@Html.ValidationMessageFor(model=>model[j].Description,“,new{@class=“text danger”})


j++; } } 阿尼亚迪尔登记处 }
控制器操作:

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();
        }
    }
}
使用aspnet_client.Models;
使用System.Collections.Generic;
使用System.Web.Mvc;
命名空间aspnet_client.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
[HttpGet]
公共操作结果AddData()
{
var记录=新列表();
添加(新数据模型());
添加(新数据模型());
添加(新数据模型());
添加(新数据模型());
添加(新数据模型());
返回视图(记录);
}
[HttpPost]
公共操作结果AddData(数据模型数据模型)
{
返回视图(数据模型);
}
[HttpGet]
公共操作结果GetDataRecords()
{
返回视图();
}
}
}
我已经更新了我的初始问题,在操作中添加了5个模型,正如您在操作代码中看到的,这似乎已经解决了我的问题(可能是更好的解决方案?)


现在的问题是更新输入图像标记中的图像名称属性和图像ID属性。我不知道该怎么做,因为我不能像上面那样使用@Html访问模型。在文本框中,我被困在后台。net使用控件的id和名称来决定如何填充模型。问题是视图为页面生成的id不足以转换为post方法所需的模型

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

    public class PageModel
    {
      /// <summary>
      /// Gets or sets the list of records.
      /// </summary>
      /// <value>
      /// The identifier.
      /// </value>
      public List<DataModel> Records { get; set; }
    }
并按如下所示更改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++;
            }
        }
获取方法

    [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);
    }
[HttpGet]
公共操作结果AddData()
{
var pm=新的页面模型();
pm.Records=新列表();
pm.Records.Add(新数据模型());
pm.Records.Add(新数据模型());
pm.Records.Add(新数据模型());
pm.Records.Add(新数据模型());
pm.Records.Add(新数据模型());
返回视图(pm);
}

这篇文章需要更多的细节。你可以发布你的数据模型,动作和整体视图吗?对不起,我已经更新了我的帖子,也许你可以帮忙谢谢。我想知道为什么在get中你发送查看记录列表,但只发回一条记录?哦,对不起,我的糟糕,只是没有完成,还没有到达,仍然需要修复;)一旦我找到一个解决办法,这是我现在的主要问题的图像。然后在记录的回发中,我想打开一个websocket连接,将数据发送到服务器,但这是另一个故事。您可能会发现以下内容很有用,建议使用IFormFile接口,在模型中安装一个字节数组。
    [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);
    }