C# Razor 页面 BindProperty 属性不起作用
C# Razor pages BindProperty attribute not working
我开始使用我的第一个 Razor 页面并按照一些示例 [BindProperty]
属性不起作用...
我正在使用 MongoDB,所以我没有在我的项目中使用 EntityFramework。
这是我的模型class:
public class Student : BaseModel
{
public string FirstName;
public string LastName;
}
[Serializable]
public abstract class BaseModel
{
[JsonProperty(Order = -2)] // appear first
[BsonRepresentation(BsonType.ObjectId)]
public virtual string Id { get; set; }
[DataType(DataType.DateTime)]
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime CreatedDate;
}
这是我的 New.cshtml.cs
代码:
public class NewModel : PageModel
{
readonly IStudentsCollection studentsCollection;
[BindProperty]
public Student Student { get; set; }
public NewModel(IStudentsCollection studentsCollection)
{
this.studentsCollection = studentsCollection;
}
public async Task<IActionResult> OnPostAsync()
{
await studentsCollection.InsertAsync(Student);
return RedirectToPage("Index");
}
}
最后,这是我的 New.cshtml
代码:
<div class="row">
<div class="col-md-4">
<form method="post">
<h4>Add New Student</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Student.FirstName"></label>
<input asp-for="Student.FirstName" class="form-control" />
<span asp-validation-for="Student.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Student.LastName"></label>
<input asp-for="Student.LastName" class="form-control" />
<span asp-validation-for="Student.LastName" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-info">Save</button>
</form>
</div>
</div>
单击 "Save" 按钮后,我进入了 OnPostAsync
方法,但是 Student
对象具有所有 null
值...
我在这里错过了什么?
您需要为 属性 提供 set
访问器:
public string FirstName { get; set; }
public string LastName { get; set; }
我开始使用我的第一个 Razor 页面并按照一些示例 [BindProperty]
属性不起作用...
我正在使用 MongoDB,所以我没有在我的项目中使用 EntityFramework。
这是我的模型class:
public class Student : BaseModel
{
public string FirstName;
public string LastName;
}
[Serializable]
public abstract class BaseModel
{
[JsonProperty(Order = -2)] // appear first
[BsonRepresentation(BsonType.ObjectId)]
public virtual string Id { get; set; }
[DataType(DataType.DateTime)]
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime CreatedDate;
}
这是我的 New.cshtml.cs
代码:
public class NewModel : PageModel
{
readonly IStudentsCollection studentsCollection;
[BindProperty]
public Student Student { get; set; }
public NewModel(IStudentsCollection studentsCollection)
{
this.studentsCollection = studentsCollection;
}
public async Task<IActionResult> OnPostAsync()
{
await studentsCollection.InsertAsync(Student);
return RedirectToPage("Index");
}
}
最后,这是我的 New.cshtml
代码:
<div class="row">
<div class="col-md-4">
<form method="post">
<h4>Add New Student</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Student.FirstName"></label>
<input asp-for="Student.FirstName" class="form-control" />
<span asp-validation-for="Student.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Student.LastName"></label>
<input asp-for="Student.LastName" class="form-control" />
<span asp-validation-for="Student.LastName" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-info">Save</button>
</form>
</div>
</div>
单击 "Save" 按钮后,我进入了 OnPostAsync
方法,但是 Student
对象具有所有 null
值...
我在这里错过了什么?
您需要为 属性 提供 set
访问器:
public string FirstName { get; set; }
public string LastName { get; set; }