发布的ViewModel始终为NULL

时间:2017-07-04 18:26:03

标签: c# asp.net-mvc null viewmodel

我的ViewModel始终返回null,但不知道原因。有人可以查看我的代码并检查这里有什么问题以及为什么我的填充模型与视图中的数据返回到控制器null

public class PaintballWorkerCreateViewModel
{
    public PaintballWorker PaintballWorker { get; set; }
    public PaintballWorkerHourlyRate HourlyRate { get; set; }
}

控制器

public ActionResult Create()
{
    PaintballWorkerCreateViewModel model = new PaintballWorkerCreateViewModel()
    {
        PaintballWorker = new PaintballWorker(),
        HourlyRate = new PaintballWorkerHourlyRate()
        {
            Date = DateTime.Now
        }
    };
    return View(model);
}

[HttpPost]
[PreventSpam(DelayRequest = 20)]
[ValidateAntiForgeryToken]
public ActionResult Create(PaintballWorkerCreateViewModel paintballWorker)
{
    (...)
}

查看,甚至添加了HiddenFor ID(不是在控制器的GET函数中创建的)。

@model WerehouseProject.ViewModels.PaintballWorkerCreateViewModel

@{
    ViewBag.Title = "Utwórz pracownika";
    Layout = "~/Views/Shared/_Layout_Paintball.cshtml";
}

<h2>Dodawanie pracownika</h2>

@using (Html.BeginForm("Create", "PaintballWorkers", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.PaintballWorker.Active)
        @Html.HiddenFor(model => model.PaintballWorker.MoneyGot)
        @Html.HiddenFor(model => model.PaintballWorker.PaintballWorkerID)
        @Html.HiddenFor(model => model.HourlyRate.Date)
        @Html.HiddenFor(model => model.HourlyRate.PaintballWorkerID)
        @Html.HiddenFor(model => model.HourlyRate.PWHourlyRateID)

        <div class="form-group">
            @Html.LabelFor(model => model.PaintballWorker.Imie, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PaintballWorker.Imie, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PaintballWorker.Imie, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PaintballWorker.Nazwisko, htmlAttributes: new { @class = "control-label col-md-2" })

(...)

        <div class="form-group">
            @Html.LabelFor(model => model.HourlyRate.HourlyRate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HourlyRate.HourlyRate, new { htmlAttributes = new { @class = "form-control", @type = "number", @min = "0.1", @step = "0.1", @value = "10" } })
                @Html.ValidationMessageFor(model => model.HourlyRate.HourlyRate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Dodaj pracownika" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

<div>
@Html.ActionLink("Powrót do listy", "Index", new object { }, new { @class = "btn btn-default" })
</div>

3 个答案:

答案 0 :(得分:0)

您的代码看起来很好..看起来它的引用正在某处丢失..您是否尝试删除[PreventSpam(DelayRequest = 20)]属性?所以你的控制器会是这样的:

public ActionResult Create()
    {
            PaintballWorkerCreateViewModel model = new PaintballWorkerCreateViewModel()
            {
                PaintballWorker = new PaintballWorker(),
                HourlyRate = new PaintballWorkerHourlyRate()
                {
                    Date = DateTime.Now
                }
            };
            return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(PaintballWorkerCreateViewModel paintballWorker)
    {
        (...)
    }

答案 1 :(得分:0)

您没有正确发布表单。由于属性是隐藏的,因此默认情况下它们为空。

发布后,您的控制器没有看到任何内容,因为隐藏了元素。因此它是空的。

使用扩展方法@ Html.TextboxFor。 Mvc viewengine将呈现文本框,然后您可以放置​​一些值并发布它们。

您还需要确保已在代码中正确映射路线。

答案 2 :(得分:0)

您的问题可能是因为命名冲突,即post动作的参数名称可能与viewmodel中的属性名称不同

请点击以下链接:

https://forums.asp.net/t/1670962.aspx?ViewModel+in+post+action+is+null

因为它明确指出了问题的解决方案和路由原因。

注意:长期以来我也遇到了同样的问题并以与上述相同的方式解决了问题。希望它对你也有用

感谢

KARTHIK