简体   繁体   English

ASP.NET Core MVC:asp-for 正在渲染不完整的 id 和 name 属性

[英]ASP.NET Core MVC: asp-for is rendering incomplete id and name attributes

When I iterate over a list in my.cshtml file, for some reason the id and name attribute values are rendered incomplete.当我遍历 my.cshtml 文件中的列表时,由于某种原因,id 和 name 属性值呈现不完整。 For example:例如:

 @for (int i = 0; i < Model.product.ProductDetail.Count(); i++)
{
    <input type="text"
    asp-for="product.ProductDetail.ElementAt(i).Sku"
    class="form-control"
    placeholder="@SharedLocalizer["Sku"]" />
}

Is rendered as:呈现为:

<input type="text" class="form-control" placeholder="Sku" id="Sku" name="Sku">

Instead of:代替:

<input type="text" id="product.ProductDetail_0__Sku" name="product.ProductDetail[0].Sku" class="form-control" placeholder="Sku">

I am using Entity Framework Core 3.1.3, here is my model:我正在使用 Entity Framework Core 3.1.3,这是我的 model:

Product:产品:

public partial class Product
{
     public Product()
     {
          ProductDetail = new HashSet<ProductDetail>();
     }

     /* More properties here... */

     public virtual ICollection<ProductDetail> ProductDetail { get; set; }
}

ProductDetail:产品明细:

public partial class ProductDetail
{
    /* More properties here... */
    public string Sku { get; set; }
}

ProductViewModel:产品视图型号:

public class ProductViewModel
{
    public Product product { get; set; }
    /* More properties here... */
}

Do you know why is this happening?你知道为什么会这样吗?

The asp-for is a ModelExpression that is evaluated in order to build up the id and name attributes. asp-for是一个ModelExpression ,它被评估以构建idname属性。 If you use ElementAt() , this expression cannot be evaluated fully.如果您使用ElementAt() ,则无法完全评估此表达式。

Try using a direct indexer int your collection instead.尝试在您的集合中使用直接索引器。 Example as follows:示例如下:

<input type="text"
asp-for="product.ProductDetail[i].Sku"
class="form-control"
placeholder="@SharedLocalizer["Sku"]" />

Edit: I strongly recommend using view models rather than data persistence level models with your views.编辑:我强烈建议在您的视图中使用视图模型而不是数据持久性级别模型。 You could then use a List in your view model (instead of an ICollection and apply the above indexing accordingly.然后,您可以在视图 model 中使用List (而不是ICollection并相应地应用上述索引。

Firstly, using asp-for="product.ProductDetail[i].Sku" as CalC shared could work for me with testing model data.首先,使用asp-for="product.ProductDetail[i].Sku"作为 CalC 共享可以对我测试 model 数据有用。

If it indeed doesn't work for you, you can try to manually set id, name and value attributes for your input fields, like below.如果它确实不适合您,您可以尝试为您的输入字段手动设置 id、name 和 value 属性,如下所示。

Here is my code of Model:这是我的 Model 代码:

products:产品:

public class Products
{
    public Product product { get; set; }
}

Product:产品:

public class Product
{
    public List<ProductDetail> ProductDetail { get; set; }
}

ProductDetail:产品明细:

public class ProductDetail
{
    public string Sku { get; set; }
}

view:看法:

<div>
    @for (int i = 0; i < Model.product.ProductDetail.Count(); i++)
    {
        <input type="text"
               asp-for="product.ProductDetail[i].Sku"
               class="form-control" 
               placeholder="@SharedLocalizer["Sku"]"/>
    }
    @for (int i = 0; i < Model.product.ProductDetail.Count(); i++)
    {
        <input type="text"
               id="product_ProductDetail_@(i)__Sku"
               name="product.ProductDetail[@(i)].Sku"
               value="@Model.product.ProductDetail.ElementAt(i).Sku"
               class="form-control"
               placeholder="@SharedLocalizer["Sku"]"/>
    }
</div>

And this is the result:这是结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 -&gt; Label asp-for 不返回 label 文本 - ASP.NET Core -> Label asp-for not returning label text 指定“asp-for”HTML标记的格式(ASP.NET Core) - Specify a format for “asp-for” HTML Tag (ASP.NET Core) 将 model 传递给 asp-for | ASP.net核心 - Passing model to asp-for | ASP.net Core 复选框中的 asp-for 抛出和 asp.net 核心中的错误 - asp-for in checkbox throws and error in asp.net core 当在剃刀页面 ASP.NET Core MVC 中与 asp-for 一起使用时,Textarea 不会在其中显示任何内容 - Textarea won't show anything inside of it when used with asp-for in razor-pages ASP.NET Core MVC ASP.NET 核心 - 如何在另一个自定义标签助手中从 asp-for 标签助手获取输入 ID - ASP.NET Core - How to get the input ID from asp-for tag helper in another custom tag helper 如何将Id和Name属性添加到ASP.NET MVC dropdownlist元素的选项? - How to add Id and Name attributes to the options for ASP.NET MVC dropdownlist element? Asp.NET Core MVC Razor页面渲染中的NotSupportedException - NotSupportedException in Asp.NET Core MVC Razor Pages Rendering 在 ASP.NET Core MVC 中一次验证两个属性 - Validating two attributes at once in ASP.NET Core MVC 将值从“选择asp-for”传递到“ textarea” asp.net核心剃须刀 - Passing value from “select asp-for” to a “textarea” asp.net core razor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM