My 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>
你的代码看起来很好......看起来它的引用在某处丢失了.. 你有没有尝试过删除[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)
{
(...)
}
您没有正确发布表单。由于属性是隐藏的,因此默认情况下它们为 null。
发布后,控制器看不到任何内容,因为元素是隐藏的。因此,它是空的。
请改用扩展方法 @Html.TextboxFor。Mvc viewengine 将呈现文本框,然后您可以放置一些值并发布它们。
您还需要确保已在代码中正确映射了路由。
您的问题可能是因为命名冲突,即 post 操作的参数名称可能与视图中的属性名称不同模型
请点击以下链接:
https://forums.asp.net/t/1670962.aspx?ViewModel+in+post+action+is+null
因为它清楚地指定了问题的解决方案和路由原因。
注意:我很久以前也遇到了同样的问题,并以与上述相同的方式解决了它。希望它对你也有用
谢谢
卡尔西克