TempData Wrapper

Since I use the same keys for TempData again and again, I would like to make it easier to keep track of these keys. It would be great if in the end I could write something like this:

MyTempData.Message = "my message";

instead

TempData["Message"] = "my message";
+5
source share
4 answers

It looks like you need a strongly typed TempData. One way to do this is to write an extension method on the controller to do this:

public static class ControllerExtensions
{
  public string GetMessage(this Controller instance)
  {
    string result = instance.TempData["Message"] as string;
    if (result == null)
    {
      result = "some default value or throw null argument exception";  
    }
    return result;
  }

  public void SetMessage(this Controller instance, string value)
  {
    instance.TempData["Message"] = value;
  }
}
+7
source

A pretty low-tech option, but if you just want to keep track of the keys you use, you can just create some constants:

public static class TempDataKeys
{
    public const string Message = "Message";
    public const string Warning = "Warning";
    public const string Error = "Error";
    // etc
}

then

TempData[TempDataKeys.Message] = "Some message";
+4
source

, , . , , ViewModels.

, ViewModel.

public class BaseViewModel
{
   public string Message{get;set;}
}

public class MyModel : BaseViewModel
{
   public string MyUniquePropety{get;set;}
}

, , , @using.

0

, ViewBag.

-, DynamicViewDataDictionary. , TempData. , Controller, WebViewPage ..

public sealed class DynamicTempDataDictionary : DynamicObject
{
    private readonly Func<TempDataDictionary> _tempDataThunk;

    public DynamicTempDataDictionary(Func<TempDataDictionary> viewDataThunk)
    {
        _tempDataThunk = viewDataThunk;
    }

    private TempDataDictionary ViewData
    {
        get { return _tempDataThunk(); }
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return ViewData.Keys;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = ViewData[binder.Name];
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        ViewData[binder.Name] = value;
        return true;
    }
}

public static class TempBagExtensions
{
    private const string TempBagKey = "__CurrentTempBagDictionary";

    public static dynamic TempBag(this ControllerBase controller)
    {
        return GetCurrentDictionary(controller.ControllerContext);
    }

    public static dynamic TempBag(this WebViewPage viewPage)
    {
        return GetCurrentDictionary(viewPage.ViewContext.Controller.ControllerContext);
    }


    private static DynamicTempDataDictionary GetCurrentDictionary(ControllerContext context)
    {
        var dictionary = context.HttpContext.Items[TempBagKey] as DynamicTempDataDictionary;

        if (dictionary == null)
        {
            dictionary = new DynamicTempDataDictionary(() => context.Controller.TempData);
            context.HttpContext.Items[TempBagKey] = dictionary;
        }

        return dictionary;
    }
}

:

this.TempBag().SomeValue = "Test";

:

@this.TempBag().SomeValue

, , Controller, , ala ViewBag.

0

All Articles