如何使用 asp-for 访问视图模型中的模型 属性?
How can I access model property inside viewmodel using asp-for?
我有这个型号
public partial class MtbWarehouse
{
public int Idhwarehouse { get; set; }
public string Namewh { get; set; }
}
我在这个视图模型中实现模型
public class WarehouseViewModel
{
public MtbWarehouse Warehouse { get; set; }
public string ListAction { get; set; } = "Index";
}
我在视图中使用 WarehouseViewModel
@model ViewModel.warehouseviewmodel
<input type="text" asp-for="Warehouse" class="form-control" placeholder="">
如何使用 asp-for
访问 Namewh
因为这个 asp-for 事情比使用
更好
@Html.TextBoxFor()
如果您在呈现视图模型的 Warehouse
属性 的 Namewh
属性 的输入文本元素之后,您可以使用点符号来访问一个子属性' 属性。像这样
<form asp-action="Index" asp-controller="Home">
<input type="text" asp-for="Warehouse.Namewh" />
<input type="submit"/>
</form>
这将生成名称为 Warehouse.Namewh
的输入文本元素,如果您从 GET 操作方法中设置该值,则会呈现该值 属性。
VS Intellisense 可能仍将其显示为红色无效。但是当执行这段代码时,标签助手将生成正确的输入字段,其值为
我有这个型号
public partial class MtbWarehouse
{
public int Idhwarehouse { get; set; }
public string Namewh { get; set; }
}
我在这个视图模型中实现模型
public class WarehouseViewModel
{
public MtbWarehouse Warehouse { get; set; }
public string ListAction { get; set; } = "Index";
}
我在视图中使用 WarehouseViewModel
@model ViewModel.warehouseviewmodel
<input type="text" asp-for="Warehouse" class="form-control" placeholder="">
如何使用 asp-for
访问 Namewh
因为这个 asp-for 事情比使用
@Html.TextBoxFor()
如果您在呈现视图模型的 Warehouse
属性 的 Namewh
属性 的输入文本元素之后,您可以使用点符号来访问一个子属性' 属性。像这样
<form asp-action="Index" asp-controller="Home">
<input type="text" asp-for="Warehouse.Namewh" />
<input type="submit"/>
</form>
这将生成名称为 Warehouse.Namewh
的输入文本元素,如果您从 GET 操作方法中设置该值,则会呈现该值 属性。
VS Intellisense 可能仍将其显示为红色无效。但是当执行这段代码时,标签助手将生成正确的输入字段,其值为