我已经查看了这些链接以供参考。
我有一些用户将填写的页面。我们会将这些页面称为Page 1
。如果他们到达了他们需要选择的字段,请下拉,但需要创建一个新项目以包含在下拉列表中,因为稍后将再次使用它们,它们会转到新页面{{1} },创建项目。创建后,他们创建了他们返回Page 2
的项目,以填写表格。问题是Page 1
现在已被删除,因为是新的页面加载。我希望这一点在他们回来时坚持下去,这样他们就不必重新填充田地了。
我目前Link2使用Cookie的路线。我不知道如何在cookie到达下一页之前设置它的信息,或者如何将它传递到该页面,因为它将转到GET方法而不是POST。
Page 1
的GET方法:
Page 1
public ActionResult Create()
{
var courseTitles = (from title in db.CourseTitles
join type in db.CourseTypes on title.Type equals type.CourseTypeID
select new
{
CourseTitleID = title.CourseTitleID,
Title = title.Title + " - " + type.Type
});
Course course = new Course();
if (Request.Cookies["CourseInfo"] != null) //If it's not null, set the model.
{
HttpCookie cookie = Request.Cookies["CourseInfo"];
course.ClassNumber = Convert.ToInt32(cookie.Values["ClassNumber"]);
course.CourseStartDate = Convert.ToDateTime(cookie.Values["StartDate"]);
course.CourseEndDate = Convert.ToDateTime(cookie.Values["EndDate"]);
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title", cookie.Values["CourseTitle"]);
return View(course);
}
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title");
return View();
}
的GET和POST方法:
Page 2
如果您需要更多代码,请与我们联系。
答案 0 :(得分:0)
您可以使用TempData
在请求之间存储对象:
public ActionResult Create()
{
var courseTitles = (from title in db.CourseTitles
join type in db.CourseTypes on title.Type equals type.CourseTypeID
select new
{
CourseTitleID = title.CourseTitleID,
Title = title.Title + " - " + type.Type
});
Course course = new Course();
if (TempData["CourseInfo"] != null) //If it's not null, set the model.
{
course = TempData["CourseInfo"] as Course;
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title", course.Title);
return View(course);
}
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title");
return View();
}
要存储课程,只需使用TempData["CourseInfo"] = course
TempData
公开了几个选项,用于定义其内容将持续多长时间。你可以阅读它here
答案 1 :(得分:0)
You could use some JavaScript to modify the GET
request to NewCourseTitle
so that it will contain the course data that the user entered.
With jQuery it could look roughly like this:
$(function () {
var newCourseTitleLink = $('#new-course-title-link');
newCourseTitleLink.on("click", function ()
{
document.location.href = newCourseTitleLink.attr('href') + '?' + $('#course-data-form').serialize();
});
});
Then you can create a cookie in your action method NewCourseTitle
:
public ActionResult NewCourseTitle(int classNumber, ... /*other form values*/)
{
var cookie = new HttpCookie("CourseInfo");
cookie.Values.Add("ClassNumber", classNumber.ToString());
...
Response.SetCookie(cookie);
ViewBag.Type = new SelectList(db.CourseTypes, "CourseTypeID", "Type");
return View();
}