复杂的场景与ASP .NET MVC 3不使用会话 [英] Complex scenario with ASP .NET MVC 3 without using Session

查看:127
本文介绍了复杂的场景与ASP .NET MVC 3不使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做楼模型类:

I have a model class called House:

public class House
{
    public House()
    {
        Residents = new List<Resident>();
    }

    public virtual string Name { get; set; }     
    ...  
    public virtual IList<Resident> Residents{ get; set; }
}

public class Resident
{
    public virtual string Name { get; set; }
    public virtual int Age{ get; set; }
    public virtual House House { get; set; }
}

所以,在我看来,创建(楼),我需要补充的居民。所以我增加了一个按钮添加居民,打开一个jQuery UI莫代尔与创建(居民),当用户点击确认,模态关闭,我Resident's电网刷新...

So, in my view Create (House), I need to add Residents . So I added a button "Add Resident", that opens a JQuery UI Modal with Create(Resident) and when the user click Confirm, the Modal closes and my Resident´s grid refresh...

我在这里的问题是,我保存列表...我没有,使用会话...但我倒是喜欢做的事,如果没有会议...

My problem here is where I save that list ... I did that using Session... But I´d like to do that without Session...

做什么从来就(我家控制器):

What I´ve done (My House Controller):

  [HttpGet]
  public ActionResult AddResident(Resident resident)  //Called when user confirms modal Resident
  {
      Residents.Add(resident);

      return PartialView("_Residents", Residents);
  }

  public Collection<Resident> Residents
  {
      get
      {
          if (Session["Residents"] == null)
          {
              var _lista = new Collection<Resident>();

              Session["Residents"] = _lista;
              return _lista;
          }
          return (Collection<Resident>)Session["Residents"];
      }
      set { Session["Residents"] = value; }
  }

所以,what's做那种情景会话不正确的方式?

So, what´s the right way to do that kind of scenario without session?

感谢

推荐答案

虽然还是老样子使用会话幕后,的TempData href=\"http://msdn.microsoft.com/en-us/library/dd394711.aspx\" rel=\"nofollow\">方式整个行动电话。

Although it stil uses the Session behind the scene, TempData is the way to go when you want to persist data across actions calls.

的操作方法可以在控制器的TempDataDictionary对象,它会调用控制器的RedirectToAction方法来调用下一个动作之前存储数据。该TempData的属性值存储在会话状态。

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state.

好的部分是,会议将您的读取后(在网格刷新方法)被自动清除。这真的是什么意思临时数据。

The good part is that the session will be automatically cleared after your read (in your grid refresh method). It's really what it means "temporary data".

下面是code与样品的TempData

Here's a sample of code with TempData:

public ActionResult AddResident(Resident resident)
{
    IList<Resident> residents = PeristResident(resident);
    return PartialView("_Residents", residents);
}

private IList<Resident> PeristResident(Resident resident)
{
    IList<Resident> residents = Residents; // this operation can empty TempData by reading it
    residents.Add(resident);
    Residents = residents; // so we persists collection after read
    return residents;
}

private IList<Resident> Residents
{
    get
    {
        object results;
        if (TempData.TryGetValue("Residents", out results) == false)
        {
            var list = new List<Resident>();
            return list;
        }
        return (IList<Resident>)results;

    }
    set { TempData["Residents"] = value; }
}

这篇关于复杂的场景与ASP .NET MVC 3不使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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