【问题标题】:View passes an empty model back to controllerView 将一个空模型传回给控制器
【发布时间】:2016-08-20 01:34:45
【问题描述】:

我在 ASP.NET 中遇到问题。我制作了一个复选框列表,我想将选定的值发布回控制器。 问题是,我在控制器中收到的模型是空的。

我的控制器:

[HttpPost]
// POST: /Checklist/index
public ActionResult Index(Zoeken systemencheckboxenss)
{
    var zoekresultaat = _db.VW_Checks.AsQueryable();

    if (systemencheckboxenss.Systemen != null)
    {
        foreach (var item in systemencheckboxenss.Systemen)
        {
            if(item.isSelected == true)
            {
               zoekresultaat = zoekresultaat.Where(sy => (sy.Systeemnaam.Contains(item.Systeemnaam)));
            }
        }
    }

    var systemen = _db.SYSTEEMs;
    //Lijst van systemen en of het geslecteerd is
    var systeemnamen = new List<CheckboxSysteem>();
    //vult de lijst

    foreach (var i in systemen)
    {
        systeemnamen.Add(new CheckboxSysteem
        {
            Systeemnaam = i.SYSTEEMNAAM,
            isSelected = false,
        });
    }

    var models = new Zoeken
    {
        VwChecks = zoekresultaat.ToList(),
        Systemen = systeemnamen,
    }; 

    return View(models);
}

这是我的观点:

@model UsabilityChecklistApp.ViewModels.Zoeken

@{
    ViewBag.Title = "Index";
}

<h2>Checks</h2>

@using (Html.BeginForm("Index", "Checklist", Model, FormMethod.Post))
{
    <table class="table">
        @foreach (var item in Model.Systemen)
        {
            <tr>
                <td>
                    @Html.HiddenFor(modelItem => item.Systeemnaam)
                    @Html.HiddenFor(modelItem => item.isSelected)
                    @Html.DisplayFor(modelItem => item.Systeemnaam)
                </td>
                <td>
                    @Html.CheckBoxFor(modelItem => item.isSelected)
                </td>

            </tr>
        }
    </table>
    <input type="submit" value="Zoek" />
}

当然还有我的 ViewModel:

using System.Web;
using UsabilityChecklistApp.Models;

namespace UsabilityChecklistApp.ViewModels
{
    public class Zoeken
    {
        public List<VW_Checks> VwChecks { get; set; }
        public List<CheckboxSysteem> Systemen { get; set; }
    }

    public class CheckboxSysteem
    {
        public string Systeemnaam { get; set; }
        public bool isSelected { get; set; }
    }
}

如果我没有提供解决问题的信息,请索取。

编辑: 我必须使用复选框,没有其他选项可能

【问题讨论】:

  • 在视图中使用 for 循环而不是 foreach

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


【解决方案1】:

默认情况下,参数使用其名称进行路由,因此它会尝试将您的模型映射到名为 Model 的参数。当您提交表单时,通常会为您处理一些事情,因此您无需担心参数名称,因为模型绑定器会处理映射。

您需要做的就是从表单中删除 Model 参数,以便模型绑定器可以为您处理这一切

@using (Html.BeginForm("Index", "Checklist", FormMethod.Post))}

要使列表正确映射,您需要像这样更改您的 cshtml:

<table class="table">
    @for(int i = 0; i < Model.Systemen.Count; i++)
    {
        <tr>
            <td>
                @Html.HiddenFor(modelItem => modelItem.Systemen[i].Systeemnaam)
                @Html.DisplayFor(modelItem => modelItem.Systemen[i].Systeemnaam)
            </td>
            <td>
                @Html.CheckBoxFor(modelItem => modelItem.Systemen[i].isSelected)
            </td>

        </tr>
    }
</table>

HTML 助手使用表达式来生成正确的标记,特别是需要具有索引才能正确映射的名称。如果您使用 foreach,则表达式不包含索引,因此生成的名称不正确,无法映射回列表。有一个superb answer to a similar question 对此进行了比以往任何时候都更好的描述。

我已经对所有这些进行了快速测试,一旦从 BeginForm 中删除 Model 参数,它就可以工作了。

【讨论】:

  • 您的第一个建议有效,但现在我收到一个空列表(调试器说 Count = 0)。我希望你能澄清为什么会发生这种情况。
  • 我认为您需要将 foreach 循环更改为 for 并将索引传递给 html 辅助方法,以便它生成正确的名称来映射数组。我已经更新了我的答案。
  • 我刚刚意识到您实际上不应该在 BeginForm 中指定您的模型,让模型绑定器为您处理。答案再次更新。 :)
  • 它把模型传给了控制器!新问题:无论我做什么,发布的值都是错误的:(
  • 那是因为@Html.HiddenFor(modelItem => modelItem.Systemen[i].isSelected),它永远不会改变,所以它总是假的,模型绑定器选择第一个来设置模型价值。如果您删除该行,它应该可以工作。
【解决方案2】:

我有同样的问题。我试过radio buttons 而不是checkbox。浏览链接

MVC3 @Html.RadioButtonfor Pass data from view to controller

View to Controller in mvc3

MVC3 @html.radiobuttonfor

How to pass List in Redirecttoaction

也许你的问题会解决。

【讨论】:

  • 我需要能够选择多个值,所以我必须使用复选框而不是单选按钮。
  • 使用checkboxes,但尝试实现我的方式。
【解决方案3】:

我重用了您的代码,并进行了更改;它完美地工作。尝试使用此代码:

型号:

 public class Zoeken
    {
        public Zoeken() { Systemen = new List<CheckboxSysteem>(); }
        public List<VW_Checks> VwChecks { get; set; }
        public List<CheckboxSysteem> Systemen { get; set; }
    }

    public class CheckboxSysteem
    {
        public string Systeemnaam { get; set; }
        public bool isSelected { get; set; }
    }

控制器:

    // GET: 
    public ActionResult Index()
    {
        var model = new Zoeken();
        CheckboxSysteem obj1 = new CheckboxSysteem();
        CheckboxSysteem obj2 = new CheckboxSysteem();
        CheckboxSysteem obj3 = new CheckboxSysteem();

        obj1.Systeemnaam = "Check1";
        obj1.isSelected = true;
        obj2.Systeemnaam = "Check3";
        obj2.isSelected = false;
        obj3.Systeemnaam = "Check3";
        obj3.isSelected = true;

        model.Systemen.Add(obj1);
        model.Systemen.Add(obj2);
        model.Systemen.Add(obj3);

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(Zoeken model)
    {         
       return View(model);
    }

查看:

@model MvcApplication2.Models.Zoeken
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            <table class="table">
                @for (int i = 0; i< Model.Systemen.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(m => m.Systemen[i].Systeemnaam)
                            @Html.HiddenFor(m => m.Systemen[i].isSelected)
                            @Html.DisplayFor(m => m.Systemen[i].Systeemnaam)
                        </td>
                        <td>
                            @Html.CheckBoxFor(m => m.Systemen[i].isSelected)
                        </td>

                    </tr>
                }
            </table>
            <input type="submit" value="Zoek" />
        }

    </div>
</body>
</html>

如果它仍然不起作用,请发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多