[英]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
,它被评估以构建id
和name
属性。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.