将ViewModel传递到Controller不能完全正常工作

时间:2019-12-18 21:24:37

标签: c# asp.net-mvc asp.net-mvc-5

我一整天都在花时间在网上寻找解决方案,但是我找不到我的错误。它变得越来越令人沮丧,我感觉到那些facepalm错误。我希望你们有时间帮助我。

我正在使用ViewModel创建视图。我正在使用BeginForm并提交输入类型按钮。现在,它传递了ViewModel中的第一个属性,但其他所有属性都传递为null。

这是我的ViewModel +模型

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,并且我可以将Workout添加到数据库(我之前已经完成了此工作),但是当我尝试使用Set或Execises时,会收到“ System.NullReferenceException:'对象引用未设置为尝试将集合添加到控制器中的锻炼时出错。

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

1 个答案:

答案 0 :(得分:1)

您的模型从这里开始是空白状态。

Workouts workout = new Workouts();

这意味着除非存在,否则您无法添加到列表。因此,当您致电

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

workout.Sets为空。您的对象永远不会对其进行初始化。

在模型中执行此操作。

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