【发布时间】:2014-02-21 01:52:16
【问题描述】:
我正在尝试创建一个简单的博客应用程序,并且我的注册和登录功能运行良好。问题是,在创建“博客”条目时,博客有一个名为标签的自定义控件,它只是该博客文章的标签数组。但是,Razor 引擎没有类似@Html.ControlFor() 的列表,所以我劫持了表单并使用 JSON 进行自己的 AJAX 调用:
{
"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");
}
问题是我在 PostBack 中的 BlogViewModel 出于某种原因除了 Title 属性之外的所有值都是空值。处理上述 JSON 对象以便我可以正确保存 BlogEntry 的正确方法是什么?
【问题讨论】:
-
你使用什么机制来反序列化 json 字符串?
-
我现在没有反序列化任何东西。我正在尝试找出最好的方法。我是否将 JSON 序列化为客户端上的字符串?还是我直接发送 JSON 对象?这是我不知道的
标签: c# .net json asp.net-mvc-4 razor