C# 模型在post上无效
我们正在尝试使用表单的提交将ViewModel传递给控制器。当ViewModel到达我们的POST方法时,它无效且为空 VraagViewModel:C# 模型在post上无效,c#,asp.net,asp.net-mvc,viewmodel,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Viewmodel,Asp.net Mvc 5,我们正在尝试使用表单的提交将ViewModel传递给控制器。当ViewModel到达我们的POST方法时,它无效且为空 VraagViewModel: public class VraagViewModel { public List<Thema> Themas { get; set; } public List<Gevolg> Condities { get; set; } public Vraag Vraag { get; set; }
public class VraagViewModel
{
public List<Thema> Themas { get; set; }
public List<Gevolg> Condities { get; set; }
public Vraag Vraag { get; set; }
public List<Gevolg> GekozenCondities { get; set; }
public Thema Thema { get; set; }
public Antwoord Antwoord { get; set; }
}
[HttpGet]
public ActionResult Index()
{
UnitOfWorkManager uow = new UnitOfWorkManager();
IThemaManager mgr = new ThemaManager(uow);
ITestManager testmgr = new TestManager(uow);
Models.VraagViewModel model = new Models.VraagViewModel
{
Themas = mgr.GetThemas().ToList(),
Condities = testmgr.GetGevolgen().ToList(),
GekozenCondities = new List<Gevolg>()
};
return View(model);
}
[HttpPost]
public void Index(Models.VraagViewModel model)
{
Debug.WriteLine(model.GekozenCondities.Count); //Nullpointer here!
if (!ModelState.IsValid)
Debug.WriteLine("ModelState not valid!");
}
@model UI.Mvc.Areas.Admin.Models.VraagViewModel
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<h2>Vraagbeheer</h2>
@using (Html.BeginForm())
{
<p>@Html.DropDownList("Themas", new SelectList(Model.Themas))</p>
<p>@Html.TextArea("vraag", "Hier komt de vraagtekst")</p>
<p>@Html.ListBox("Condities1", new SelectList(Model.Condities), new { id =
"condities1" })</p>
<p>@Html.ListBox("Condities2", new SelectList(Model.GekozenCondities), new {
id = "condities2" })</p>
<p><button type="button" id="add" class="btn btn-default">Conditie
toevoegen</button></p>
<p><button type="button" id="remove" class="btn btn-default"
onclick="verwijderConditie">Conditie verwijderen</button></p>
@Html.TextArea("antwoord1", "Voor- en nadelen\nAntwoord1")
@Html.TextArea("antwoord2", "Voor - en nadelen\nAntwoord2")
@Html.TextArea("antw1Kort", "Antwoord 1")
@Html.TextArea("antw2Kort", "Antwoord 2")
<input id="gevolgInvullen" type="submit" value="Gevolgen invullen"
class="btn btn-default" />
}
@section scripts{
<script type="text/javascript">
$(function () {
$("#add").bind("click", function (e) {
$("#condities1 > option:selected").each(function () {
$(this).remove().appendTo("#condities2");
});
e.preventDefault();
});
$("#remove").bind("click", function (e) {
$("#condities2 > option:selected").each(function () {
$(this).remove().appendTo("#condities1");
});
e.preventDefault();
});
/*$("gevolgInvullen").bind("click", function () {
Url.Action("Action", "Controller")
})*/
});
</script>
}
公共类VraagViewModel
{
公共列表主题{get;set;}
公共列表条件{get;set;}
公共Vraag Vraag{get;set;}
公共列表GekozenCondities{get;set;}
公共主题{get;set;}
公共Antwoord Antwoord{get;set;}
}
控制器:
public class VraagViewModel
{
public List<Thema> Themas { get; set; }
public List<Gevolg> Condities { get; set; }
public Vraag Vraag { get; set; }
public List<Gevolg> GekozenCondities { get; set; }
public Thema Thema { get; set; }
public Antwoord Antwoord { get; set; }
}
[HttpGet]
public ActionResult Index()
{
UnitOfWorkManager uow = new UnitOfWorkManager();
IThemaManager mgr = new ThemaManager(uow);
ITestManager testmgr = new TestManager(uow);
Models.VraagViewModel model = new Models.VraagViewModel
{
Themas = mgr.GetThemas().ToList(),
Condities = testmgr.GetGevolgen().ToList(),
GekozenCondities = new List<Gevolg>()
};
return View(model);
}
[HttpPost]
public void Index(Models.VraagViewModel model)
{
Debug.WriteLine(model.GekozenCondities.Count); //Nullpointer here!
if (!ModelState.IsValid)
Debug.WriteLine("ModelState not valid!");
}
@model UI.Mvc.Areas.Admin.Models.VraagViewModel
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<h2>Vraagbeheer</h2>
@using (Html.BeginForm())
{
<p>@Html.DropDownList("Themas", new SelectList(Model.Themas))</p>
<p>@Html.TextArea("vraag", "Hier komt de vraagtekst")</p>
<p>@Html.ListBox("Condities1", new SelectList(Model.Condities), new { id =
"condities1" })</p>
<p>@Html.ListBox("Condities2", new SelectList(Model.GekozenCondities), new {
id = "condities2" })</p>
<p><button type="button" id="add" class="btn btn-default">Conditie
toevoegen</button></p>
<p><button type="button" id="remove" class="btn btn-default"
onclick="verwijderConditie">Conditie verwijderen</button></p>
@Html.TextArea("antwoord1", "Voor- en nadelen\nAntwoord1")
@Html.TextArea("antwoord2", "Voor - en nadelen\nAntwoord2")
@Html.TextArea("antw1Kort", "Antwoord 1")
@Html.TextArea("antw2Kort", "Antwoord 2")
<input id="gevolgInvullen" type="submit" value="Gevolgen invullen"
class="btn btn-default" />
}
@section scripts{
<script type="text/javascript">
$(function () {
$("#add").bind("click", function (e) {
$("#condities1 > option:selected").each(function () {
$(this).remove().appendTo("#condities2");
});
e.preventDefault();
});
$("#remove").bind("click", function (e) {
$("#condities2 > option:selected").each(function () {
$(this).remove().appendTo("#condities1");
});
e.preventDefault();
});
/*$("gevolgInvullen").bind("click", function () {
Url.Action("Action", "Controller")
})*/
});
</script>
}
[HttpGet]
公共行动结果索引()
{
UnitOfWorkManager uow=新的UnitOfWorkManager();
ITHEMA经理=新经理(uow);
ITestManager testmgr=新的测试管理器(uow);
Models.VraagViewModel model=新模型.VraagViewModel
{
Themas=mgr.gethemas().ToList(),
Condities=testmgr.GetGevolgen().ToList(),
GekozenCondities=新列表()
};
返回视图(模型);
}
[HttpPost]
公共无效索引(Models.VraagViewModel model)
{
Debug.WriteLine(model.GekozenCondities.Count);//此处为空指针!
如果(!ModelState.IsValid)
Debug.WriteLine(“ModelState无效!”);
}
Index.cshtml:
public class VraagViewModel
{
public List<Thema> Themas { get; set; }
public List<Gevolg> Condities { get; set; }
public Vraag Vraag { get; set; }
public List<Gevolg> GekozenCondities { get; set; }
public Thema Thema { get; set; }
public Antwoord Antwoord { get; set; }
}
[HttpGet]
public ActionResult Index()
{
UnitOfWorkManager uow = new UnitOfWorkManager();
IThemaManager mgr = new ThemaManager(uow);
ITestManager testmgr = new TestManager(uow);
Models.VraagViewModel model = new Models.VraagViewModel
{
Themas = mgr.GetThemas().ToList(),
Condities = testmgr.GetGevolgen().ToList(),
GekozenCondities = new List<Gevolg>()
};
return View(model);
}
[HttpPost]
public void Index(Models.VraagViewModel model)
{
Debug.WriteLine(model.GekozenCondities.Count); //Nullpointer here!
if (!ModelState.IsValid)
Debug.WriteLine("ModelState not valid!");
}
@model UI.Mvc.Areas.Admin.Models.VraagViewModel
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<h2>Vraagbeheer</h2>
@using (Html.BeginForm())
{
<p>@Html.DropDownList("Themas", new SelectList(Model.Themas))</p>
<p>@Html.TextArea("vraag", "Hier komt de vraagtekst")</p>
<p>@Html.ListBox("Condities1", new SelectList(Model.Condities), new { id =
"condities1" })</p>
<p>@Html.ListBox("Condities2", new SelectList(Model.GekozenCondities), new {
id = "condities2" })</p>
<p><button type="button" id="add" class="btn btn-default">Conditie
toevoegen</button></p>
<p><button type="button" id="remove" class="btn btn-default"
onclick="verwijderConditie">Conditie verwijderen</button></p>
@Html.TextArea("antwoord1", "Voor- en nadelen\nAntwoord1")
@Html.TextArea("antwoord2", "Voor - en nadelen\nAntwoord2")
@Html.TextArea("antw1Kort", "Antwoord 1")
@Html.TextArea("antw2Kort", "Antwoord 2")
<input id="gevolgInvullen" type="submit" value="Gevolgen invullen"
class="btn btn-default" />
}
@section scripts{
<script type="text/javascript">
$(function () {
$("#add").bind("click", function (e) {
$("#condities1 > option:selected").each(function () {
$(this).remove().appendTo("#condities2");
});
e.preventDefault();
});
$("#remove").bind("click", function (e) {
$("#condities2 > option:selected").each(function () {
$(this).remove().appendTo("#condities1");
});
e.preventDefault();
});
/*$("gevolgInvullen").bind("click", function () {
Url.Action("Action", "Controller")
})*/
});
</script>
}
@model UI.Mvc.Areas.Admin.Models.VraagViewModel
@{
ViewBag.Title=“Index”;
}
Vraagbeheer
@使用(Html.BeginForm())
{
@Html.DropDownList(“Themas”,新选择列表(Model.Themas))
@Html.TextArea(“vraag”,“Hier komt de vraagtekst”)
@Html.ListBox(“条件1”,新建SelectList(Model.Condities),新建{id=
“条件1”})
@ListBox(“条件2”,新建SelectList(Model.GekozenCondities),新建{
id=“条件2”})
条件
托沃根
康迪蒂·维尔维德伦酒店
@text区域(“antwoord1”、“Voor-en nadelen\nAntwoord1”)
@TextArea(“antwoord2”、“Voor-en nadelen\nAntwoord2”)
@text区域(“antw1Kort”、“Antwoord 1”)
@TextArea(“antw2Kort”、“antwoord2”)
}
@节脚本{
$(函数(){
$(“#添加”).bind(“单击”),函数(e){
$(“#条件1>选项:选中”)。每个(函数(){
$(this.remove().appendTo(“#条件2”);
});
e、 预防默认值();
});
$(“#删除”).bind(“单击”,函数(e){
$(“#条件2>选项:选中”)。每个(函数(){
$(this.remove().appendTo(“#条件1”);
});
e、 预防默认值();
});
/*$(“gevolginvulen”).bind(“单击”,函数(){
动作(“动作”、“控制器”)
})*/
});
}
问题
我们想知道当ViewModel到达控制器中的POST方法时,是什么导致ViewModel为null
编辑1
似乎模型本身在您的表单中没有名为:GekozenCondities的字段,因此它当然将为null 在您的表单中,它被称为条件2
<p>@Html.ListBox("Condities2", new SelectList(Model.GekozenCondities), new {
id = "condities2" })</p>
@Html.ListBox(“条件2”,新选择列表(Model.GekozenCondities),新{
id=“条件2”})
换成,
<p>@Html.ListBox("GekozenCondities", new SelectList(Model.GekozenCondities), new {
id = "condities2" })</p>
@Html.ListBox(“gekozencodities”,新选择列表(Model.gekozencodities),新{
id=“条件2”})
但我认为这无论如何都不会起作用,因为它只会发布1个值(从下拉列表中选择的值),但在您的模型中,它是一个列表
阅读表单基础知识?发布后,您的模型为空,因为在视图中,表单内部不包含任何VraagViewModel类的属性。您需要指定适当控件的名称 e、 假设这是你们班
public class VraagViewModel
{
public Antwoord Antwoord { get; set; }
}
public class Antwoord
{
public string Name { get; set; }
}
然后在您的视图中,您的控件名称将是Antwoord.name
像这样
然后,您可以使用model.Antwoord.Name在操作中访问它。您是否确认该模型为null,而不是其GekozenCondities属性?您可以发布获得的确切错误消息。该模型本身似乎不是null。模型中的所有内容都为null,除了GekozenCondities和Themas(计数均为0)。视图正在生成与模型完全没有关系的表单控件。当您从未为其生成任何表单控件时,您如何期望
GekozenCondities
包含任何内容。我强烈建议你买一本好书,学习基础知识。它是一个列表框
输入,因此可以传递多个选定值。很酷,在这种情况下,应该可以解决它!