I’m using Razor pages for my ASP.NET project. After cloning the repository, it started giving one specific build error :
Error (active)
CS1061 List< CaseFileStatus > does not contain a definition for ‘CreatedTime_’ and no accessible extension method ‘CreatedTime_’ accepting a first argument of type ‘List< CaseFileStatus >’ could be found (are you missing a using directive or an assembly reference?)
Here is the codeblock which gives the error :
@model List<CaseFileStatus>
@if (Model.Any())
{
foreach (var item in Model)
{
....
<div class="col-md-10">
<input name="CreatedTime" id="[email protected]" class="form-control" value="@item.CreatedTime.ToString("yyyy-MM-ddThh:mm")" />
<span asp-validation-for="[email protected]" class="text-danger"></span> <---- this is the line with the error
</div>
....
And here is the model :
public class CaseFileStatus {
....
public int ID { get; set; }
public int CaseFileID { get; set; }
public DateTime ModifiedTime { get; set; }
public DateTime CreatedTime { get; set; }
.....
Developer of the project told and show me that he is not having any issues with that usage so I’m beginning to think I have the problem in my project or machine and it started to drive me crazy.
Clean+rebuilding, restarting project or my computer didn’t solve my problem.
4
If you want to post the list model to backend with received parameter(List<CaseFileStatus> model
) like below:
[HttpPost]
public IActionResult Index(List<CaseFileStatus> model)
{
//do your stuff...
return View(model);
}
Model Binding binds the property by name attribute. The correct name for list model should be:[index].PropertyName
. asp-validation-for
tag helper expects a model property name, but you’re trying to dynamically create an identifier using Razor syntax, which isn’t valid in this context. You need change your View like below:
@if (Model.Any())
{
int i = 0;
foreach (var item in Model)
{
<div class="col-md-10">
<input name="[@i].CreatedTime" id="[email protected]" class="form-control" value="@item.CreatedTime.ToString("yyyy-MM-ddThh:mm")" />
<span asp-validation-for="@Model[@i].CreatedTime" class="text-danger"></span>
</div>
i++;
}
}