帖子上的模型无效

时间:2017-04-21 11:29:59

标签: 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 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!");                 
    }

Index.cshtml:

@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>
}

问题

我们想知道当ViewModel到达我们控制器的POST方法时,是什么原因导致ViewModel为null。

编辑1

似乎模型本身is not null.

2 个答案:

答案 0 :(得分:0)

您的表单中没有名为GekozenCondities的字段,因此它当然会为空...

在您的表单中,它被称为Condities2

<p>@Html.ListBox("Condities2", new SelectList(Model.GekozenCondities), new { 
id = "condities2" })</p>

将其更改为;

<p>@Html.ListBox("GekozenCondities", new SelectList(Model.GekozenCondities), new { 
id = "condities2" })</p>

但我认为这无论如何都不会起作用,因为它只会发布1个值(下拉列表中的选定的值),但在你的模型中它是一个列表......

阅读表格的基础知识?

答案 1 :(得分:0)

您的模型在发布后为空,因为在视图,内部表单中,它不包含 VraagViewModel 类的任何属性。您需要指定适当的控件名称。

例如,假设这是你的班级。

public class VraagViewModel
    {
        public Antwoord Antwoord { get; set; }
    }
    public class Antwoord
    {
        public string Name { get; set; }
    }

然后在您的视图中,您的控件名称将为 Antwoord.Name 喜欢这个

<input type="text" name="Antwoord.Name"/>

OR

 @Html.TextBoxFor(x => x.Antwoord.Name)

然后,您可以使用 model.Antwoord.Name

在您的操作中访问它