我正在嘗試創建一個簡單的博客應用程序,並且我的註冊和登錄功能正常工作。問題是,當創建一個「Blog」條目時,博客有一個名爲Tags的自定義控件,它只是該博客文章的一個標籤數組。然而,剃刀引擎沒有一個@Html.ControlFor()
像一個名單的事情,所以我劫持的形式,做使用JSON我自己的AJAX調用:.NET MVC 4 HTTP將數據發佈爲JSON對象
{
"Title" : "mytitle",
"Content" : "some content",
"Tags" : ["tag1", "tag2", "tag3"]
}
我有一個BlogViewModel,看起來像這樣:
public class BlogViewModel
{
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Required]
[Display(Name = "Content")]
public string Content { get; set; }
public List<Tag> Tags { get; set; }
}
和控制器的動作:
[HttpPost]
public ActionResult Create(BlogViewModel bvm)
{
/* This is where I want to build of a BlogEntry object and save it to the DB */
return RedirectToAction("Index");
}
的事情是,我在回傳BlogViewModel具有除了Title屬性由於某種原因,所有的空值。處理上述JSON對象的正確方法是什麼,以便我可以正確保存BlogEntry?
什麼是你用來反序列化json字符串的機制? – Rakhita
我現在不反序列化任何東西。我想弄清楚最好的方法..我是否將JSON序列化爲客戶端上的字符串?還是直接發送JSON對象?這是我不知道 –