C# 将ViewModel传递给控制器不会';我不能完全工作
我花了一整天的时间在网上搜索问题的解决方案,但是由于我的错误,我找不到解决方案。这是相当令人沮丧,我有一种感觉,这是一个脸掌的错误。我希望你们能抽出时间来帮我 我正在创建一个使用ViewModel的视图。我使用的是一个通知和提交输入类型按钮。现在,它传递ViewModel中的第一个属性,但其他所有属性都传递为null 这是我的视图模型+模型C# 将ViewModel传递给控制器不会';我不能完全工作,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我花了一整天的时间在网上搜索问题的解决方案,但是由于我的错误,我找不到解决方案。这是相当令人沮丧,我有一种感觉,这是一个脸掌的错误。我希望你们能抽出时间来帮我 我正在创建一个使用ViewModel的视图。我使用的是一个通知和提交输入类型按钮。现在,它传递ViewModel中的第一个属性,但其他所有属性都传递为null 这是我的视图模型+模型 using System; using System.Collections.Generic; using System.Linq; using Syste
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>
Execes还没有被编码进去
总而言之。视图可以传递ViewModel,并且我可以将训练添加到数据库中(我以前已经这样做过),但是,当我尝试使用集合或Execes时,我在控制器中尝试将集合添加到训练中时会出现“System.NullReferenceException:“Object reference not set to a instance of a Object.”错误
我希望我已经详细描述了它。如果没有,请询问更多信息。您的模型一开始只是一张白板
Workouts workout = new Workouts();
这意味着除非列表存在,否则无法将其添加到列表中。所以当你打电话的时候
workout.Sets.Add(model.Sets); // NullReferenceException here.
训练集为空。你的对象从不初始化它
在您的模型中执行此操作
public List<Sets> Sets { get; set; } = new List<Sets>();
公共列表集{get;set;}=new List();
相反,如果有时您不想初始化它(ORM绑定很生气或者其他什么),请确保在使用它之前调用它,就像这样
Workouts workout = new Workouts();
workout = model.Workout;
workout.Sets = new List<Sets>();
workout.Sets.Add(model.Sets);
Workouts锻炼=新锻炼();
锻炼=模型锻炼;
workout.set=新列表();
锻炼。设置。添加(模型。设置);
这是否回答了您的问题?你真漂亮!工作起来很有魅力!非常感谢你!!
Workouts workout = new Workouts();
workout = model.Workout;
workout.Sets = new List<Sets>();
workout.Sets.Add(model.Sets);