mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
Add helpers to SettingsDictionary
--HG-- branch : dev
This commit is contained in:
parent
483f2f842a
commit
d8a1478200
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.ContentManagement.MetaData.Models {
|
||||
@ -6,11 +7,7 @@ namespace Orchard.ContentManagement.MetaData.Models {
|
||||
public SettingsDictionary() { }
|
||||
public SettingsDictionary(IDictionary<string, string> dictionary) : base(dictionary) { }
|
||||
|
||||
public T GetModel<T>() where T : class, new() {
|
||||
return GetModel<T>(typeof(T).Name);
|
||||
}
|
||||
|
||||
public T GetModel<T>(string key) where T : class, new() {
|
||||
private T TryGetModel<T>(string key) where T : class {
|
||||
var binder = new DefaultModelBinder();
|
||||
var controllerContext = new ControllerContext();
|
||||
var context = new ModelBindingContext {
|
||||
@ -18,7 +15,24 @@ namespace Orchard.ContentManagement.MetaData.Models {
|
||||
ModelName = key,
|
||||
ValueProvider = new DictionaryValueProvider<string>(this, null)
|
||||
};
|
||||
return (T)binder.BindModel(controllerContext, context) ?? new T();
|
||||
return (T)binder.BindModel(controllerContext, context);
|
||||
|
||||
}
|
||||
|
||||
public T GetModel<T>() where T : class, new() {
|
||||
return GetModel<T>(typeof(T).Name);
|
||||
}
|
||||
|
||||
public T GetModel<T>(string key) where T : class, new() {
|
||||
return TryGetModel<T>(key) ?? new T();
|
||||
}
|
||||
|
||||
public bool ContainsModel<T>() where T : class {
|
||||
return TryGetModel<T>(typeof(T).Name) != null;
|
||||
}
|
||||
|
||||
public bool ContainsModel<T>(string key) where T : class {
|
||||
return TryGetModel<T>(key) != null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user