[英]ASP.MVC Settings Object Model Binding
我想為每個用戶存儲一個設置對象 (不要設置表),但是我認為我的競爭方式是錯誤的。 那就是我試圖完成的方式:
// this is the user model
public class AppUser: IdentityUser
{
public string FirstName {get; set;}
public string LastName {get; set;}
public UserSettings Settings {get; set;} // the desired object
}
// the settings class
[DataContract]
public class UserSettings
{
[DataMember]
public bool ANiceProperty {get; set;}
[DataMember]
public enum AnotherNiceEnumPropery {get; set;}
[DataMember]
public UserSettingsSubClass SubClass {get; set;}
// and so on...
}
// another sub-settings class
[DataContract]
public class UserSettingsSubClass
{
[DataMember]
public bool ANiceProperty {get; set;}
}
最好的使用方式是什么,例如User.Settings.Property? 在瘋狂的PHP和MySQL時代,我討厭創建一個數組-序列化-以字符串存儲-反序列化-返回數組。 但是我認為有更好的方法,不是嗎?
無論如何,您必須將設置序列化為字符串。 我建議您使用Json.Net庫
用法示例:
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.