将视图模型传递到控制器不能完全工作



我一直在网络上搜索解决方案,但是我找不到我的错误。它变得非常令人沮丧,我有一种感觉,那就是那些脸上的错误。我希望你们能找到时间帮助我。

我正在研究使用视图模型的创建视图。我正在使用开始表单并提交输入类型按钮。现在,它传递视图模型中的第一个属性,但所有其他属性都作为 null 传递。

这是我的观点模型+模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FitnessSundhed.Models;
namespace FitnessSundhed.ViewModels
{
public class WorkoutViewModel
{
public Workouts Workout { get; set; }
public Sets Sets { get; set; }
public Execises Execises { get; set; }
}
}

其中一些属性应该列出,但我选择不将它们列出,直到我尝试过以防出现问题。别介意。

这是锻炼模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FitnessSundhed.Models
{
public class Workouts
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string Description { get; set; }
public string Equipment { get; set; }
public string Targets { get; set; }
public List<Sets> Sets { get; set; }

}
}

集模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FitnessSundhed.Models
{
public class Sets
{
public int Id { get; set; }
public string SetName { get; set; }
public string SetDesc { get; set; }
public List<Execises> Execises { get; set; }
}
}

执行模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace FitnessSundhed.Models
{
public class Execises
{
public int Id { get; set; }
public string ImagePath { get; set; }
public string Name { get; set; }
public int Reps { get; set; }
public string Note { get; set; }
}
}

基本上,锻炼包含一个包含练习列表的集合列表。

视图

@model FitnessSundhed.ViewModels.WorkoutViewModel
@{
ViewBag.Title = "Action";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Action</h2>
@using (Html.BeginForm("Create", "Workouts", FormMethod.Post, null))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Workouts</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Workout.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Workout.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Workout.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Workout.Author, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Workout.Author, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Workout.Author, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Workout.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Workout.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Workout.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Workout.Equipment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Workout.Equipment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Workout.Equipment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Workout.Targets, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Workout.Targets, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Workout.Targets, "", new { @class = "text-danger" })
</div>
</div>

<h4>Sets</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Sets.SetName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Sets.SetName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Sets.SetName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sets.SetDesc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Sets.SetDesc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Sets.SetDesc, "", new { @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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

控制器

[HttpPost]
public ActionResult Create(WorkoutViewModel model)
{
Workouts workout = new Workouts();
workout = model.Workout;
workout.Sets.Add(model.Sets); // NullReferenceException here.
_context.Workoutss.Add(workout);
_context.SaveChanges();
return RedirectToAction("Library", "Workouts");
}

执行器尚未编码。

总而言之。视图可以通过 ViewModel,我可以将锻炼添加到数据库中(我以前已经这样做过(,但是当我尝试使用集合或执行时,我收到"System.NullReferenceException:"对象引用未设置为对象的实例"错误当我尝试将集合添加到控制器中的锻炼时。

我希望我已经详细描述了它。如果没有,请询问更多信息。

您的模型在这里开始时是一张白纸。

Workouts workout = new Workouts();

这意味着除非列表存在,否则无法添加到列表。所以当你打电话

workout.Sets.Add(model.Sets); // NullReferenceException here.

锻炼。集合为空。您的对象永远不会初始化它。

在模型中执行此操作。

public List<Sets> Sets { get; set; } = new List<Sets>();

相反,如果有时你不希望它初始化(ORM绑定生气或其他什么(,请确保在使用它之前调用它,就像这样。

Workouts workout = new Workouts();
workout = model.Workout;
workout.Sets = new List<Sets>();
workout.Sets.Add(model.Sets); 

最新更新