Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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#_Html_Asp.net Mvc_Razor - Fatal编程技术网

C# 如何使用ASP.NET MVC在具有相同属性的多个文本框上显示多个验证错误?

C# 如何使用ASP.NET MVC在具有相同属性的多个文本框上显示多个验证错误?,c#,html,asp.net-mvc,razor,C#,Html,Asp.net Mvc,Razor,我正在编写一个鞋子订购系统,在本例中,我有一个视图,您可以在其中添加一组特定尺寸的鞋子和库存。我的代码工作正常,但没有验证,我不知道如何验证我想避免使用[httpPost]插入时重复鞋码 我有一组使用列表具有相同属性的文本框: 我期望达到的目标: 这就是我所拥有的: 视图模型: public class TallaViewModel { public Talla Talla { get; set; } = new Talla(); public List&

我正在编写一个鞋子订购系统,在本例中,我有一个视图,您可以在其中添加一组特定尺寸的鞋子和库存。我的代码工作正常,但没有验证,我不知道如何验证我想避免使用
[httpPost]
插入时重复鞋码

我有一组使用列表具有相同属性的文本框:

我期望达到的目标:

这就是我所拥有的:

视图模型:

    public class TallaViewModel
{

    public Talla Talla { get; set; } = new Talla();
  


    public List<Talla> Tallitas { get; set; } = new List<Talla>() 
    { new Talla() { }, new Talla() { }, new Talla() { } , new Talla() { } , new Talla() { }, new Talla() { }, new Talla() { }, new Talla() { }, new Talla() { }  };


    public List<DetalleTalla> Detallitos { get; set; } = new List<DetalleTalla>()
    { new DetalleTalla() { }, new DetalleTalla() { }, new DetalleTalla() { } , new DetalleTalla() { } , new DetalleTalla() { }, new DetalleTalla() { }, new DetalleTalla() { }, new DetalleTalla() { }, new DetalleTalla() { }  };
}
公共类TallaViewModel
{
public-Talla-Talla{get;set;}=new-Talla();
公共列表Tallitas{get;set;}=new List()
{new Talla(){},new Talla(){},new Talla(){},new Talla(){},new Talla(){},new Talla(){},new Talla(){},new Talla(){},new Talla(){};
公共列表Detallitos{get;set;}=new List()
{new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){},new DetalleTalla(){};
}
Tallas(上下文,我使用的是实体框架):

公共类塔拉
{
公共塔拉()
{
DetalleTallas=新列表();
}
公共int Id{get;set;}
公共字符串NombreTalla{get;set;}
公共ICollection DetalleTallas{get;set;}
}
视图:


@对于(int i=0;is.Tallitas[i].NombreTalla,新的{@class=“form control”,@placeholder=“Talla”})
@Html.ValidationMessageFor(s=>s.Tallitas[i].NombreTalla,“,new{@class=“text danger”})
}
@对于(int i=0;is.Detallitos[i].Stock,新{@class=“form control”,@placeholder=“Talla”})
}
控制器:

[HttpPost]
    public ActionResult TallasAgregar(TallaViewModel viewModel) 
    {
        for (int i = 0; i < viewModel.Tallitas.Count; i++)
        {

            var talla = viewModel.Tallitas[i];

            if (talla.NombreTalla == null)
            {
                continue;
            }


            _tallaRepository.AddSinGuardar(talla);


            var detalletalla = new DetalleTalla()
            {
                ProductoId = viewModel.Ids,
                Stock = viewModel.Detallitos[i].Stock,
                TallaId = talla.Id
            };

            _tallaRepository.DetalleSinGuardar(detalletalla);


            _tallaRepository.Guardar();
        }

        return RedirectToAction("DetallesProducto", new { id = viewModel.Ids });
    }
[HttpPost]
公共行动结果塔拉萨格雷加(塔拉维模型视图模型)
{
对于(int i=0;i
我尝试使用NombreTalla避免重复,并根据视图在列表中的位置将验证错误返回给视图。如果至少有一个验证错误,则不会发生插入。如何解决这个问题?

您可以试试这个。
                            <div class="card-body">
                            <div class="row">

                                <div class="col-md-6">
                                    @for (int i = 0; i < Model.Tallitas.Count; i++)
                                    {

                                <div class="col-sm-12">
                                    <div class="form-group">
                                        <label>Talla</label>
                                        @Html.TextBoxFor(s => s.Tallitas[i].NombreTalla, new { @class = "form-control", @placeholder = "Talla" })
                                        @Html.ValidationMessageFor(s => s.Tallitas[i].NombreTalla, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                                      }
                                </div>

                                <div class="col-md-6">


                                    @for (int i = 0; i < Model.Detallitos.Count; i++)
                                    {

                                <div class="col-sm-12">
                                    <div class="form-group">
                                        <label>Stock</label>
                                        @Html.TextBoxFor(s => s.Detallitos[i].Stock, new { @class = "form-control", @placeholder = "Talla" })

                                    </div>
                                </div>}
                                </div>


                            </div>
                        </div>
[HttpPost]
    public ActionResult TallasAgregar(TallaViewModel viewModel) 
    {
        for (int i = 0; i < viewModel.Tallitas.Count; i++)
        {

            var talla = viewModel.Tallitas[i];

            if (talla.NombreTalla == null)
            {
                continue;
            }


            _tallaRepository.AddSinGuardar(talla);


            var detalletalla = new DetalleTalla()
            {
                ProductoId = viewModel.Ids,
                Stock = viewModel.Detallitos[i].Stock,
                TallaId = talla.Id
            };

            _tallaRepository.DetalleSinGuardar(detalletalla);


            _tallaRepository.Guardar();
        }

        return RedirectToAction("DetallesProducto", new { id = viewModel.Ids });
    }