简体   繁体   English

MVC 4将视图模型传递给控制器​​-使用JavaScriptSerializer反序列化复杂的JSON对象

[英]MVC 4 Passing View Model to controller - Using JavaScriptSerializer to Deserialize complex JSON Object

I am trying to Deserialise a JSON object in to a strongly typed .net object/Type. 我正在尝试将JSON对象反序列化为强类型的.net对象/类型。 This is the object I want to create from the JSON 这是我要从JSON创建的对象

[Serializable]
public class CartItem
{
    [Key]
    public int Id { get; set; }
    public virtual Product Product { get; set; }
    public virtual List<SelectedProductOption> SelectedProductOptions { get; set; }
    public virtual Cart Cart { get; set; }
    public int Quantity { get; set; }
}

So you have the whole picture, I am also going to post the definition for the other objects that make up CartItem, but remember Its CartItem I want to ultimately end up with 因此,您已经了解了整个情况,我还将发布组成CartItem的其他对象的定义,但请记住,我最终希望将其定义为CartItem

[Serializable]
public class Product
{
    [Key]
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public virtual Category Category { get; set; }
}


[Serializable]
public class Category
{
    [Key]
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public string ImageURL { get; set; }
}

In my CartItem Object I have a collection of SelectedProductOption (THIS IS WHERE I AM HAVING PROBLEMS) This is the definition for a SelectedProductOption 在我的CartItem对象中,我有一个SelectedProductOption的集合(这是我遇到的问题),这是SelectedProductOption的定义

[Serializable]
public class SelectedProductOption
{
    public Option Option { get; set; }
    public AllowedOptionValue Value { get; set; }
}
    [Serializable]
public class Option
{
    [Key]
    public int Id { get; set; }
    public string OptionName { get; set; }
    public virtual OptionGroup OptionGroup { get; set; }
}

    [Serializable]
    public class AllowedOptionValue
{
    [Key]
    public int Id { get; set; }
    public virtual Option Option { get; set; }
    public string OptionValue { get; set; }
    public string OptionCaption { get; set; }
    public decimal? PriceOffSet { get; set; }
    public bool? IsPercentageOffSet { get; set; }
 }

Now this is the JSON Object that I am trying to serialize 现在这是我要序列化的JSON对象

{\"Product\":{\"Id\":1,\"Price\":3.5,\"ProductName\":\"Rice Meal One\",\"Category\":         {\"Id\":1,\"CategoryName\":\" Rice Meals\",\"ImageURL\":\" \"},\"SelectedProductOptions\":[{\"Option\":{\"Id\":1,\"OptionName\":\"OptionName1\",\"OptionGroup\":{\"Id\":1,\"OptionGroupName\":\"Option Group\"}},\"Value\":{\"Id\":1,\"Option\":{\"Id\":4,\"OptionName\":\"OptionName2\",\"OptionGroup\":{\"Id\":1,\"OptionGroupName\":\"Option Group2\"}},\"OptionValue\":\"12123123\",\"OptionCaption\":\"12123123\",\"PriceOffSet\":3.2,\"IsPercentageOffSet\":true}},{\"Option\":{\"Id\":1,\"OptionName\":\"dsa\",\"OptionGroup\":{\"Id\":1,\"OptionGroupName\":\"asdas\"}},\"Value\":{\"Id\":1,\"Option\":{\"Id\":1,\"OptionName\":\"dsa\",\"OptionGroup\":{\"Id\":1,\"OptionGroupName\":\"asdas\"}},\"OptionValue\":\"12123123\",\"OptionCaption\":\"12123123\",\"PriceOffSet\":3.2,\"IsPercentageOffSet\":true}}]}}

So when I call: 因此,当我致电:

CartItem cartItem = js.Deserialize<CartItem>(formData); 

CartItem.Product has the right values CartItem.Product.Category has the Right Values CartItem.Product具有正确的值CartItem.Product.Category具有正确的值

but

CarItem.SelectedProductOptions is null... CarItem.SelectedProductOptions为null ...

Where am I going wrong? 我要去哪里错了? the only property that is not working, any ideas on how to fix it? 唯一不起作用的属性,如何解决它的任何想法?

There is no need to do it manually, asp.net MVC will do it for you :) 无需手动进行此操作,asp.net MVC会为您完成:)

In view create a form, which contains all needed fields: 在视图中创建一个表单,其中包含所有必需的字段:

@model CartItem; @model CartItem;

<form action="/mycontroller/myaction">

    @Html.TextBoxFor(m => mode.ProductName)
    ...

    <button type=submit>OK</button>    
</form>

and define your controllers action like: 并定义您的控制器操作,例如:

[HttpPost]
public ActionResult myaction(CartItem carItem)
{
 ...
}

the carItem object should contain all the data that you entered in the view carItem对象应包含您在视图中输入的所有数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM