保持页面状态 [英] Keeping a page's state

查看:98
本文介绍了保持页面状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了这些链接以供参考.

I have already looked at these links for references.

我有几页用户需要填写.我们将这些页面称为 Page 1 .如果他们到达需要从中选择的字段,则进行下拉,但需要创建一个新项目以包含在下拉菜单中,因为稍后将再次使用该项目,因此他们将转到新页面 Page2 ,以创建商品.创建完成后,他们创建商品,并返回到 Page 1 完成表单的填写.问题在于 Page 1 现在已被擦除,因为这是新的页面加载.我希望这种情况在他们回来时仍然有效,这样他们就不必填写字段了.

I have a few pages that a user will be filling out. We will call these pages Page 1. If they get to a field that they need to select from, drop down, but need to create a new item to be included in the drop down, because it will be used again later, they go to a new page, Page 2, to create the item. After create they create the item they are returned to Page 1 to finishing filling out the form. The problem is that the Page 1 is now erased because is a new page load. I would like for this to persist for when they come back so they don't have to refill out fields.

我当前正在使用Cookie的路由 Link2 .我不知道如何在到达下一页之前设置cookie的信息,或者在进入GET方法而不是POST之前如何将其传递到该页面.

The route I am currently Link2 using a cookie. I don't know how to set the cookie's info before it gets to the next page, or how to pass it to that page before since it is going to a GET method and not a POST.

第1页的的GET方法:

GET method for 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();
}

第2页的的GET和POST方法:

GET and POST method for Page 2:

public ActionResult NewCourseTitle()
{
    ViewBag.Type = new SelectList(db.CourseTypes, "CourseTypeID", "Type");
    return View();
}

//
//Post:

[HttpPost]
public ActionResult NewCourseTitle(CourseTitle courseTitle)
{
    if (ModelState.IsValid)
    {
        db.CourseTitles.AddObject(courseTitle);
        db.SaveChanges();
        return RedirectToAction("Create", "Course");
    }
    return View();
}

让我知道您是否需要更多代码.

Let me know if you need more code.

推荐答案

您可以使用一些JavaScript将 GET 请求修改为 NewCourseTitle ,以便包含课程用户输入的数据.使用jQuery时,它可能大致如下所示:

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();
    });

});

然后,您可以在操作方法 NewCourseTitle 中创建一个cookie:

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();
}

这篇关于保持页面状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆