我有一个名为ResultsViewModel的viewmodel,我曾经在asp.net core mvc 1.1中强烈输入我的创建视图。它有5个下拉菜单和一些输入字段,如下面的代码所示:
这是ViewModel
public class ResultsViewModel
{
public ResultsViewModel()
{
}
[Display(Name = "Students")]
public SelectList Students { get; set; }
[Display(Name = "Subjects")]
public SelectList Subjects { get; set; }
[Display(Name = "Term")]
public Term Terms { get; set; }
[Display(Name = "Continuous Assessment")]
public SelectList ContinuousAssessments { get; set; }
[Display(Name = "Continuous Assessment Score")]
[DisplayFormat(DataFormatString ="{0:F2}")]
public double CAScore { get; set; }
[Display(Name = "School Year")]
public SelectList Session { get; set; }
[Display(Name = "Class")]
public SelectList Class { get; set; }
[Display(Name = "Average Score")]
[DisplayFormat(DataFormatString = "{0:F2}")]
public double AverageScore { get; set; }
[Display(Name = "Term Total Score")]
[DisplayFormat(DataFormatString = "{0:F2}")]
public double TermTotal { get; set; }
}
这是创建视图。它的标准没什么特别的
@model SwiftSchool.ViewModels.ResultsViewModel
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<form asp-action="Create" method="post">
<div class="form-horizontal">
<h4>Result</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Students" class="col-md-2 control-label"></label>
<div class="col-md-10">
@Html.DropDownList("Student", Model.Students, "Select Student", new { @class = "form-control", @id = "studentselect" })
</div>
</div>
<div class="form-group">
<label asp-for="Subjects" class="col-md-2 control-label"></label>
<div class="col-md-10">
@Html.DropDownList("Subject", Model.Subjects, "Select Subject", new { @class = "form-control", @id = "subjectselect" })
</div>
</div>
<div class="form-group">
<label asp-for="Session" class="col-md-2 control-label"></label>
<div class="col-md-10">
@Html.DropDownList("Session", Model.Session, "Select School Session",
new { @class = "form-control", @id = "sessionselect" })
</div>
</div>
<div class="form-group">
<label asp-for="Class" class="col-md-2 control-label"></label>
<div class="col-md-4">
@Html.DropDownList("Class", Model.Class, "Select Student Class",
new { @class = "form-control", @id = "classselect" })
</div>
</div>
<div class="form-group">
<label asp-for="ContinuousAssessments" class="col-md-2 control-label"></label>
<div class="col-md-10">
@Html.DropDownList("C.A", Model.ContinuousAssessments, "Select C.A",
new { @class = "form-control", @id = "caselect" })
</div>
</div>
<div class="form-group">
<label asp-for="CAScore" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="CAScore" class="form-control" readonly id="cascore" />
<span asp-validation-for="CAScore" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="AverageScore" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="AverageScore" class="form-control" />
<span asp-validation-for="AverageScore" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="TermTotal" class="col-md-2 control-label"</label>
<div class="col-md-10">
<input asp-for="TermTotal" class="form-control" />
<span asp-validation-for="TermTotal" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"/>
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
我有表单提交并将ResultsViewModel返回给我的控制器。创建视图下拉列表的两种方法和处理POST的方法如下所示:
// GET: Result/Create
public IActionResult Create()
{
var rim = new ResultsViewModel();
rim.Students = new SelectList(_context.Students.ToList(), "Id", "FullName");
rim.Subjects = new SelectList(_context.Subjects.ToList(), "Id", "Name");
rim.ContinuousAssessments = new SelectList(_context.ContinuousAssessment.ToList(), "Id", "Name");
rim.Class = new SelectList(_context.Classes.ToList(), "Id", "ClassName");
rim.Session = new SelectList(_context.Sessions.ToList(), "Id", "SessionName");
return View(rim);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ResultsViewModel model)
{
if (ModelState.IsValid)
{
_context.Add(model);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(model);
}
}
因此,当我在选择并填充表单上的所有字段后对表单进行POST时,我设置了一个断点,我检查ModelState是否有效,并检查注入Create方法的viewmodel,一切都是null,特别是下拉列表。所以一切都从那里向南走。有人可以让我知道发生了什么事吗?我发现这很烦人。我希望它不是框架cos中的另一个错误我在网上看到这是早期版本的框架中的一个错误。无论如何,任何帮助将不胜感激。
答案 0 :(得分:1)
您创建的下拉列表与模型中的任何属性都没有关系。
第一个绑定到名为Student
的属性,该属性不存在。第二个绑定到名为Subject
的属性,该属性也不存在。第3个绑定到属性Session
,它是复杂对象的集合(IEnumerable<SelectListItem>
),但<select>
元素仅回发单个值 - 所选选项的值。同上第4位。
您的模型需要绑定到
的属性[Display(Name = "Students")]
[Required("Please select a student")]
public int SelectedStudent { get; set; }
public IEnumerable<SelectListItem> Students { get; set; }
然后在视图中
@Html.DropwDownListFor(m => m.SelectedStudent, Model.Students, "Select Student", new { @class = "form-control" })
请注意,不清楚为什么使用new { @id = "studentselect" }
覆盖方法生成的默认id
属性(id="SelectedStudent"
)
或者,使用Tag Helper
<select asp-for="SelectedStudent" asp-items="Model.Students" class="form-control">
<option>Select Student</option>
</select>
使用生成的3个输入将绑定正常,假设您为这些属性类型提交了有效值。
附注:在POST方法中,您需要在将模型返回到视图之前重新填充SelectLists,否则它们将为null
并抛出异常 - 请参阅The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable'