我正在使用用户登录的MVC网站,可以在活动表单中输入信息。提交后,它会显示结果。假设此人转到网站上的其他链接,然后决定返回活动表单。有没有办法让我的用户的表单字段,下拉框,滑块放置等仍然保持?表单不应保留的唯一时间是用户注销。
到目前为止,这是我的代码:
查看 - 活动
@model Project.Models.ActivityModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(x => x.Activity_CityName)
<div class="editor-label">
<strong>@Html.LabelFor(x => x.Activity_StartDate)</strong>
</div>
<div class="editor-field">
<input id="checkin" type="text" name="Activity_StartDate" />
</div>
@Html.TextBoxFor(x => x.Activity_EndDate)
@Html.DropDownListFor(x => x.Activity_NumAdults, AdultNum)
@Html.DropDownListFor(x => x.Activity_NumChildren)
@Html.DropDownListFor(x => x.Activity_ChildAge1, ChildAge)
@Html.DropDownListFor(x => x.Activity_ChildAge2, ChildAge)
@Html.DropDownListFor(x => x.Activity_ChildAge3, ChildAge)
<div class="submitbutton">
<input data-inline="true"type="submit" id="activity_search" value="Search" />
</div>
}
结果视图 - Activities_FormSuccess
@model Project.Models.ActivityModel
<p>City: @Model.Activity_CityName</p>
<p>StartDate: @Model.Activity_StartDate</p>
<p>EndDate: @Model.Activity_EndDate</p>
<div>
<p><strong>Ticket</strong></p>
<p>Number of Adults: @Model.Activity_NumAdults</p>
<p>Number of Children: @Model.Activity_NumChildren</p>
<p>Child 1 age: @Model.Activity_ChildAge1</p>
<p>Child 2 age: @Model.Activity_ChildAge2</p>
<p>Child 3 age: @Model.Activity_ChildAge3</p>
</div>
控制器
public ActionResult Activities()
{
return View();
}
[HttpPost]
public ActionResult Activities(ActivityModel activitysubmission)
{
return View("Activities_FormSuccess", activitysubmission);
}
答案 0 :(得分:2)
我处理这个的方式是在控制器函数之间传递的会话对象(也可能是维护用户登录状态的地方)。当用户点击“稍后完成此表单”或其他任何内容时,将表单,表单的值或表单序列化为JSON保存到会话中。每当表单页面加载时,检查会话中是否存在该变量;如果是,请使用该数据预先填写表格;否则,显示空白。
答案 1 :(得分:2)
您应该将发布的模型存储在会话中的帖子操作中,并在向用户显示表单时将其恢复。
public ActionResult Activities()
{
if(Session["Activities"] != null)
{
var activityModel = (ActivityModel)Session["Activities"];
return View(activityModel );
}
return View();
}
[HttpPost]
public ActionResult Activities(ActivityModel activitysubmission)
{
...
Session["Activities"] = activitysubmission;
return View("Activities_FormSuccess", activitysubmission);
}
答案 2 :(得分:1)
将您想要的信息存储在某种形式的“会话”值中,然后在返回此视图时检查其中是否存在,并填写(如果适用)。