私の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)
{
(...)
}
ビュー、(コントローラにGET
関数で作成されていない)もHiddenFor追加のID。
@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>
あなたのために役立つことを願っていますと、それはすべての値がnullであるか、または少数の値だけですか? –
'PaintballWorker'と' HourlyRate'はモデルを作成して 'GET'関数のデータで埋めてもnullです。 – Cezar
2つのクラスのコードを表示できますか? –