Asp.net core mvc Asp.NET MVC 6,TagHelper“;asp代表;以身作则

Asp.net core mvc Asp.NET MVC 6,TagHelper“;asp代表;以身作则,asp.net-core-mvc,tagbuilder,Asp.net Core Mvc,Tagbuilder,假设我有这样一个局部观点: @model DateTime? <input asp-for="???" class="form-control" /> @model DateTime? 什么去了???要将其自身绑定到模型?它看起来像是asp for tag helper设置了输入html标记的“name”和“value”属性。如果您的模型是简单类型或行为类似于值类型(如string或DateTime?)的复杂类型,则asp for helper无法设置“name”属性。因此,您的

假设我有这样一个局部观点:

@model DateTime?
<input asp-for="???" class="form-control" />
@model DateTime?

什么去了???要将其自身绑定到模型?

它看起来像是asp for tag helper设置了输入html标记的“name”和“value”属性。如果您的模型是简单类型或行为类似于值类型(如string或DateTime?)的复杂类型,则asp for helper无法设置“name”属性。因此,您的选择是:

@model DateTime?
<input name="mytime" value="@Model" class="form-control" />
或者,如果您坚持使用asp进行,那么您可以使用一些小技巧:

@model DateTime?
@{
    var mytime = Model;
}
<input asp-for="@mytime" class="form-control" />
@model DateTime?
@{
var mytime=模型;
}
迟到总比不迟到好,我想:)

使用:

<input asp-for="@Model" class="form-control" />

<input asp-for="@Model" class="form-control" />