i在模型中具有不可自然的属性。如果Model State IsInvalid提交表单时,我希望用户重定向到同一页面。但是,每当ModelState无效时,我都会得到例外,例如
例外
参数字典包含一个无效类型的参数'referalId'system.int32'for Method'System.web.mvc.m.mvc.madresult referralConfirnation(int32)'中的参数'referalId'。可选参数必须是参考类型,可随机的类型,或称为可选参数。 参数名称:参数
模型:
public class ReferrerInstanceViewModel
{
[Key]
public int Id { get; set; }
[FileType("pdf|doc|docx|PDF", ErrorMessage = "File type is not valid.")]
[Required]
public HttpPostedFileBase ProofDoc { get; set; }
[Required]
public int ReferralId { get; set; }
[Required]
public int? ReferralStatusId { get; set; }
public IEnumerable<SelectListItem> ReferralStatuses { get; set; }
}
控制器动作:
public ActionResult ReferralConfirmation(int referralId)
{
var viewModel = new ReferrerInstanceViewModel
{
ReferralId = referralId,
ReferralStatuses = _context.ReferralStatuses.Select(x => new SelectListItem
{
Text = x.ReferralStatusType,
Value = x.ReferralStatusId.ToString()
})
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ReferralConfirmation(ReferrerInstanceViewModel viewModel)
{
if (!ModelState.IsValid)
return RedirectToAction("ReferralConfirmation", viewModel.ReferralId);
// Some Logic
return RedirectToAction("ReferrerCenter");
}
查看:
@using (Html.BeginForm("ReferralConfirmation", "Referrer", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m=>m.ReferralId)
<div class="form-horizontal">
<h4>ReferrerInstanceViewModel</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ReferralStatusId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ReferralStatusId, Model.ReferralStatuses, "Reject Or Refer", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReferralId, "*", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProofDoc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.ProofDoc, new { type = "file" })
@Html.ValidationMessageFor(model => model.ProofDoc, "*", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
注意:
- 当ModelState无效时,我正在调用
RedirectToAction("ReferralConfirmation", I AM PASSING THE REQUIRED ReferralId VALUE HERE)
- 当模型状态无效时,即使是httpget转介确认行动的断点也不会受到打击。
我无法理解这种行为。我在做什么错。
请注意,ModelState是无效的URL是
https://localhost:44328/Referrer/ReferralConfirmation
用对象调用 RedirectToAction
时,您应该通过属性名称与您的转介申请书的参数名称匹配的匿名对象
return RedirectToAction("ReferralConfirmation",
new { referralId = viewModel.ReferralId } );
这将返回带有位置值的302响应,因为/ReferralConfirmation?referralId=100
假设ViewModel中的值。ReferralId是100
同样,当模型状态无效时,进行重定向没有意义。您应该返回相同的视图,以便用户可以修复视图并重新提交。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ReferralConfirmation(ReferrerInstanceViewModel viewModel)
{
if (ModelState.IsValid)
{
//to do : Your code before the redirect goes here
return RedirectToAction("ReferralConfirmation",
new { referralId = viewModel.ReferralId });
}
//model validation failed. Let's return to same view
return View(viewModel);
}