2014-02-21 31 views
0

我正在嘗試創建一個簡單的博客應用程序,並且我的註冊和登錄功能正常工作。問題是,當創建一個「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?

+0

什麼是你用來反序列化json字符串的機制? – Rakhita

+0

我現在不反序列化任何東西。我想弄清楚最好的方法..我是否將JSON序列化爲客戶端上的字符串?還是直接發送JSON對象?這是我不知道 –

回答

0

實際上有一種方法可以通過Razor綁定到模型。這裏是你如何做到這一點:

@for (int i = 0; i < Model.Tags.Count; i++) 
{ 
    @Html.TextboxFor(model => model.Tags[i].TagName) 
} 

假設你的標籤模型是這樣的:

public class Tag 
{ 
    public string TagName { get; set; } 
} 

這將標籤的集合綁定到你的模型。不過,我不確定這是否是解決問題的正確方法,因爲我假設您要動態添加X個標籤。對?

+0

是的,但在博客的創建,Model.Tags.Count是0 ..所以我如何創建一個新的。 –

+0

就像我說過,如果你動態添加它們,那麼你最好的選擇是使用一些現有的jQuery標籤庫,如下所示: http://welldonethings.com/tags/manager http://aehlke.github.io/tag -it/ – Marko

0

嘗試了這一點: -

使用IModelBinder接口

public class Tag 
{ 
    public string TagName { get; set; } 
} 

[Serializable()] 
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; } 
} 

public class BlogViewModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     BlogViewModel model; 

     if (controllerContext.RequestContext.HttpContext.Request.AcceptTypes.Contains("application/json")) 
     { 
      var serializer = new JavaScriptSerializer(); 
      var form = controllerContext.RequestContext.HttpContext.Request.Form.ToString(); 
      model = serializer.Deserialize<BlogViewModel>(HttpUtility.UrlDecode(form)); 
     } 
     else 
     { 
      model = (BlogViewModel)ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, bindingContext); 
     } 

     return model; 
    } 
} 

JS: -

<script type="text/javascript"> 
    var $tags = []; 

    var tag = function (TagName) { 
     this.TagName = TagName; 
    } 

    $tags.push(new tag("a")); 
    $tags.push(new tag("b")); 
    $tags.push(new tag("c")); 

    jQuery(function ($) { 
     $.ajax({ 
      url:"http://localhost:3938/Blog/Create", 
      ContentType: "application/json; charset=utf-8", 
      dataType:"json", 
      type: "post", 
      data:JSON.stringify({ 
         "Title" : "mytitle", 
         "Content" : "some content", 
         "Tags": $tags 
        }) 
     }); 
    }); 

</script> 

和你的Global.asax.cs內部文件中添加ModelBinders.Binders[typeof(BlogViewModel)] = new BlogViewModelBinder();

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     ModelBinders.Binders[typeof(BlogViewModel)] = new BlogViewModelBinder(); 
    } 
} 
+0

@MattHintzke這是否回答您的查詢? –

0

嘗試在您的ajax調用中添加此項。

data: JSON.stringify(model), 
contentType: 'application/json'