我的ViewModel總是返回null
,不知道爲什麼。有人可以看看我的代碼,並檢查這裏有什麼不對,以及爲什麼我的填充模型與視圖中的數據返回到控制器爲null
?發佈ViewModel總是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>
是不是所有值都爲空或只有幾個值? –
'PaintballWorker'和'HourlyRate'爲空,即使我創建了模型並在GET函數中填充了數據。 – Cezar
你可以顯示2個類的代碼嗎? –