MVC路由选项在页面

时间:2016-06-26 22:54:27

标签: asp.net-mvc model-binding

我的MVC控制器中有以下服务器端代码,它是使用模型绑定的基本post,get,redirect模式。

问题是,在我提交页面后,它会正确地发送路由选项bool参数的RedirectToAction。但是当我重新加载页面(F5)时,它仍然发送路由选项参数(bool)作为前一个值(true),当我希望它发送null时。似乎价值以某种方式持续存在。

有关如何解决此问题的任何想法?非常感谢。

控制器代码:

    [HttpGet]
    public ActionResult DischargeHuman(bool? HumanDischarged = null)
    {
        DischargeHumanViewModel dischargeHumanVM = new DischargeHumanViewModel();
        dischargeHumanVM.HumanDischarged = HumanDischarged;
        return View(dischargeHumanVM);
    }

    [HttpPost]
    public ActionResult DischargeHuman(DischargeHumanViewModel dischargeHumanVM)
    {
        string username = HttpContext.User.Identity.Name.Substring(HttpContext.User.Identity.Name.LastIndexOf(@"\")).Trim('\\');
        dischargeHumanVM.UserName = username;

        if (ModelState.IsValid)
        {
            dischargeHumanVM.HumanDischarged = _adminTaskLogic.DischargeHuman(dischargeHumanVM.ClientID.Value, dischargeHumanVM.HumanID, dischargeHumanVM.UserName, dischargeHumanVM.DateOfDischarge.Value);
        }


        if (dischargeHumanVM.HumanDischarged.Value)
        {
            return RedirectToAction("DischargeHuman", new { dischargeHumanVM.HumanDischarged });
        }
        else
        {
            return View(dischargeHumanVM);
        }
    }

.cshtml代码:

@using (Html.BeginForm())
{
    @Html.ValidationSummary();

    if (Model.HumanDischarged.HasValue)
    {
        if (Model.HumanDischarged.Value)
        {
            <span style="color:blue" id="msgSpan">Human successfully discharged.</span>
        }
        else
        {
            <span style="color:red" id="msgSpan">An error occurred.</span>
        }
        Model.PatientDischarged = null;
    }
...

查看模型:

public class DischargePatientViewModel
{
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Client: ")]
    public int? ClientID { get; set; }

    [Required]
    [Display(Name = "Patient ID: ")]
    public string PatientID { get; set; }

    [Required]
    [Display(Name = "Date Of Discharge: ")]
    public DateTime? DateOfDischarge { get; set; }

    public bool? PatientDischarged { get; set; }

    public IEnumerable<SelectListItem> SelectClientList
    {
        get { return new SelectList(new ClientLogic().GetClients(null, false, true), "ClientID", "ClientDisplayText"); }
    }
}

0 个答案:

没有答案