我从我的第一个Razor页面开始,并在一些示例之后,[BindProperty]
属性不起作用...
我正在使用MongoDB,所以我不在项目中使用EntityFramework。
这是我的Model课:
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>
点击“保存”按钮后,我进入OnPostAsync
方法,但是Student
对象具有所有null
值...
我在这里想念什么?
答案 0 :(得分:1)
您需要为您的媒体资源提供set
访问者:
public string FirstName { get; set; }
public string LastName { get; set; }