从存储的会话填充表单



我有一个剃刀形式:

   @Html.TextBoxFor(m=> m.SystolicBT, new{@class="RiskScoreTextBox"})
   @Html.TextBoxFor(m=> m.PersonWeight, new{@class="RiskScoreTextBox"})
   @Html.TextBoxFor(m=> m.PersonWeight, new{@class="RiskScoreTextBox"})

然后我有了这个方法,它可以检索对象并将其存储在会话中。

  [HttpPost]
    public ActionResult CreatePrototype(DementiaPrototypeModel Prototype)
    {
        Session["DemensPrototype"] = Prototype;
        return RedirectToAction("RiskScore");
    }

然后我被发送到这个方法:

  [HttpGet]
    public ViewResult RiskScore()
    {
        var prototype = Session["DemensPrototype"];
        return View();
    }

在这里,如果我接管原型,我可以使用对象,但现在我有了一个类似的表单,我想用存储的对象信息填充它。我该怎么做?

您可以这样做:

var prototype = Session["DemensPrototype"] as DementialPrototypeModel;
return View(prototype);

或者,这个:

[HttpPost]
    public ActionResult CreatePrototype(DementiaPrototypeModel Prototype)
    {
        return RedirectToAction("RiskScore", Prototype);
    }
[HttpGet]
    public ViewResult RiskScore(DementiaPrototypeModel prototype)
    {
        return View(prototype);
    }

只需确保您的视图期望DementialPrototypeModel对象作为其模型。

最新更新