HttpPost操作方法中的视图模型参数为空
本文关键字:参数 模型 视图 操作方法 HttpPost | 更新日期: 2023-09-27 18:03:02
我重定向我的viewModel从View1的HttpPost到View2的HttpGet。这工作没有问题。在那里,用户必须接受条款和协议。这应该在viewModel中更改为true(在它为false之前)。然后重定向到view2的HttpPost。
出问题了。View2的HttpPost ActionResult接收所有参数为NULL的viewModel(在它们被填充之前)
我该如何解决这个问题?
这是我的HttpGet ActionResult for View2:
public ActionResult Verify(QuestionViewModel viewModel)
{
//Anrede in Viewbag
if (viewModelVeri.Per_Salutation == 2)
{
ViewBag.Per_Salutation = "Frau";
}
else
{
ViewBag.Per_Salutation = "Herr";
}
int? per_region_id = viewModelVeri.Per_Region;
int per_region_id_nullable = Convert.ToInt32(per_region_id);
Region region = _repository.GetRegionById(per_region_id_nullable);
QuestionViewModel viewModel2 = new QuestionViewModel()
{
Reg_Name = region.Reg_Name
};
//Regionsname in Viewbag
ViewBag.Reg_Name = viewModel2.Reg_Name;
return View(viewModel);
}
我的HttpPost ActionResult for View2:
[HttpPost]
public ActionResult Verify(QuestionViewModel viewModel, string tbButton)
{
//here the ViewModel-Parameters are already NULL
我的观点:
<div class="panel-body">
@using (Html.BeginForm("Verify", "QuestionForm", FormMethod.Post, new { id = "verifyform" }))
{
@Html.AntiForgeryToken()
<div class="ctrl-row">
<div class="form-group">
<div class="container-fluid">
@Html.LabelFor(model => model.Per_Salutation, new { @class = "control-label col-sm-1" })
<div class="col-sm-3">
@ViewBag.Per_Salutation
</div>
</div>
</div>
</div>
<div class="ctrl-row">
<div class="form-group">
<div class="container-fluid">
@Html.LabelFor(model => model.Per_Name_Last, new { @class = "control-label col-sm-1" })
<div class="col-sm-3">
@Html.DisplayFor(model => model.Per_Name_Last, new { @class = "control-label col-sm-1 non-zero-num" })
</div>
@Html.LabelFor(model => model.Per_Name_First, new { @class = "control-label col-sm-1" })
<div class="col-sm-3">
@Html.DisplayFor(model => model.Per_Name_First, new { @class = "control-label col-sm-1 non-zero-num" })
</div>
</div>
</div>
</div
<div class="ctrl-row">
<div class="form-group">
<div class="container-fluid">
@Html.LabelFor(model => model.Per_EMail, new { @class = "control-label col-sm-1" })
<div class="col-sm-8">
@Html.DisplayFor(model => model.Per_EMail, new { @class = "control-label col-sm-1 non-zero-num" })
</div>
</div>
</div>
</div>
<div class="checkbox">
<input type="checkbox" id="NutzungsbedingungenAngenommen " />
<label for="NutzungsbedingungenAngenommen ">
Ich erkläre mich mit den Nutzungsbedingungen einverstanden.
</label>
</div>
<button class="btn btn-default" type="submit" name="tbButton" value="questsend">Senden</button>
}
<script>
$(document).ready(function () {
$(".non-zero-num").val($(this).val() == 0 ? ' ' : $(this).val());
})
$('#verifyform').on('click', '[value="questsend"]', function () {
if ($('#agree').is(':checked')) {
return true;
}
else {
return false;
}
});
</script>
编辑
这里是我的QuestionViewModel
public class QuestionViewModel
{
//Other Properties
[Required(ErrorMessage = "Bitte die Nutzungsbedingungen annehmen!")]
public bool NutzungsbedingungenAngenommen { get; set; }
}
My HttpPost Controller for View1:
[HttpPost]
public ActionResult DefaultForm(QuestionViewModel viewModel, string tbButton)
{
if (ModelState.IsValid)
{
try
{
if (tbButton.Equals("questsend"))
{
return RedirectToAction("Verify", viewModel);
}
else if (tbButton.Equals("questupload"))
{
//write to DB
return View(viewModel);
}
else
{
dropdownPopulate(viewModel);
return View("DefaultForm", viewModel);
}
}
catch
{
dropdownPopulate(viewModel);
return View(viewModel);
}
}
else
{
dropdownPopulate(viewModel);
return View(viewModel);
}
}
问题是您使用Html.DisplayFor
来显示View2中viewModel
的属性值,因此这些值不会提交给HttpPost方法,因此当HttpPost for View2执行时,viewModel
为空。只提交<input>
, <textarea>
和<select>
标签中的值。
您可以通过在Html.BeginForm
中添加Html.HiddenFor
来为viewModel
的所有属性提交viewModel
的值给HttpPost。您还应该使用Html.CheckBoxFor(m => m.NutzungsbedingungenAngenommen)
作为复选框。如下所示
@using (Html.BeginForm("Verify", "QuestionForm", FormMethod.Post, new { id = "verifyform" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.Per_Salutation)
@Html.HiddenFor(m => m.Per_Name_First)
@Html.HiddenFor(m => m.Per_Name_Last)
.... // Html.HiddenFor for the rest of QuestionViewModel properties
....
.... // the rest of your code inside the form tag
.... // remove <input type="checkbox" id="NutzungsbedingungenAngenommen " />
@Html.CheckBoxFor(m => m.NutzungsbedingungenAngenommen)
<button class="btn btn-default" type="submit" name="tbButton" value="questsend">Senden</button>
}
-
确保你声明了Razor应该使用的模型。
@model QuestionViewModel
-
确保HTML输入的名称和id是MVC模型绑定器所期望的格式。我建议使用提供的HtmlHelpers,而不是手工编写输入标签。
@Html.CheckBoxFor(m => m.Agree)
-
从POST动作中删除
string tbButton
参数