我将viewModel从View1的HttpPost重定向到View2的HttpGet。 这没有问题。 用户必须接受条款和协议。 这应该在viewModel中更改为true(在它为false之前)。 然后重定向到view2的HttpPost。
出了点问题。 View2的HttpPost ActionResult接收viewModel,所有参数都为NULL(填充之前)
我该如何解决这个问题?
这是我对View2的HttpGet ActionResult:
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);
}
这是我对View2的HttpPost ActionResult:
[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; }
}
我的View1的HttpPost控制器:
[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);
}
}
答案 0 :(得分:0)
确保声明Razor应使用的模型。
@model QuestionViewModel
确保HTML输入的名称和ID采用MVC modelbinder预期的格式。我建议使用提供的HtmlHelpers而不是手工编写输入标签。
@Html.CheckBoxFor(m => m.Agree)
从POST操作中删除string tbButton
参数
答案 1 :(得分:0)
问题是您使用Html.DisplayFor
在View2中显示viewModel
的属性值,因此不会将值提交给HttpPost方法,因此viewModel
为null当HttpPost for View2被执行时。只会提交<input>
,<textarea>
和<select>
标记中的值。
您可以通过在viewModel
内为Html.HiddenFor
的所有属性添加Html.BeginForm
,将viewModel
的值提交给HttpPost for View2。您还应该使用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>
}