Asp.Net Core 1.1 ViewModel Null after Form Post



我有一个viewModel,我称之为resuctsviewModel,我用来在ASP.NET Core MVC 1.1中强烈键入我的Create View。如下所示,它有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");}
}

我的表单将我的结果视图模型归还给我的控制器。为视图创建我的下拉列表和处理帖子的方法的两种方法如下:

// 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);
    }
}

因此,当我选择和填充表单上的所有字段后,我在表格上发表帖子时,我设置了一个断路点,请访问ModelState是否有效,并检查注入创建方法的ViewModel,所有内容都是零,尤其是下拉菜。所以一切都从那里向南。有人可以让我知道发生了什么事吗?因为我觉得这很烦人。我希望在框架上的某个地方没有另一个错误,我看到这是框架早期版本中的错误。无论如何,任何帮助都会赞赏。

您的创建与模型中任何属性没有关系的dropdownlists。

第一个绑定到不存在的名为Student的属性。第二个绑定到名为Subject的属性,该属性再次不存在。第三次与属性Session结合,该属性是复杂对象的集合(IEnumerable<SelectListItem>),但<select>元素仅发布一个值 - 所选选项的值。同上第四。

您的模型需要属性才能绑定到

[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"

另外,使用标签助手

<select asp-for="SelectedStudent" asp-items="Model.Students" class="form-control">
    <option>Select Student</option>
</select>

假设您提交了这些属性类型的有效值,则使用使用的3个输入将限制罚款。

附带注意:在帖子方法中,您需要在将模型返回视图之前重新填充选择列表,否则它们将是 null,并且将抛出一个例外 - 请参阅具有键'xxx'is的ViewData项目类型为" system.int32",但必须是类型为" iEnumerable"

相关内容

  • 没有找到相关文章

最新更新