My Model is : (Problematic section is Variant[] Variants, that is not passing to controller)
public class SingleProduct
{
public Product1 product { get; set; }
}
public class Product1
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("body_html")]
public string Body_html { get; set; }
[JsonProperty("vendor")]
public string Vendor { get; set; }
[JsonProperty("product_type")]
public string Product_type { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("variants")]
public Variant1[] Variants { get; set; }
}
public class Variant1
{
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("weight")]
public double Weight { get; set; }
[JsonProperty("inventory_quantity")]
public int Inventory_quantity { get; set; }
[JsonProperty("weight_unit")]
public string Weight_unit { get; set; }
}
Currently using this View Code to Create :
@model SingleProduct
<form asp-action="PostApi">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
@Html.LabelFor(model => model.product.Title, new { @class = "control-label" })
@Html.TextBoxFor(model => model.product.Title, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.product.Title)
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Body_html, new { @class = "control-label"})
@Html.TextBoxFor(model => model.product.Body_html, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.product.Body_html)
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Product_type, new { @class = "control-label" })
@Html.TextBoxFor(model => model.product.Product_type, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.product.Product_type)
<input type="hidden" name="product.Status" value="active" />
<input type="hidden" name="product.Variants.Weight_unit" value="lb" />
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Variants.SingleOrDefault().Price, new { @class = "control-label", @required = "required" })
@Html.TextBoxFor(model => model.product.Variants.SingleOrDefault().Price, new { @class = "form-control", @required = "required" })
@Html.ValidationMessageFor(model => model.product.Variants.SingleOrDefault().Price)
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Variants.SingleOrDefault().Compare_at_price, new { @class = "control-label", @required = "required" })
@Html.TextBoxFor(model => model.product.Variants.SingleOrDefault().Compare_at_price, new { @class = "form-control", @required = "required" })
@Html.ValidationMessageFor(model => model.product.Variants.SingleOrDefault().Compare_at_price)
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Variants.SingleOrDefault().Inventory_quantity, new { @class = "control-label", @required = "required" })
@Html.TextBoxFor(model => model.product.Variants.SingleOrDefault().Inventory_quantity, new { @class = "form-control", @required = "required" })
@Html.ValidationMessageFor(model => model.product.Variants.SingleOrDefault().Inventory_quantity)
</div>
<div class="form-group">
@Html.LabelFor(model => model.product.Variants.SingleOrDefault().Weight, new { @class = "control-label" })
@Html.TextBoxFor(model => model.product.Variants.SingleOrDefault().Weight, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.product.Variants.SingleOrDefault().Weight)
</div>
<br/>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
My Controller is: (I want to recieve all data binded to model, but Varient list does not appears)
public ActionResult PostApi(SingleProduct model)
{
//Here model does not outputs Variants Data...
}
Here is the sample of my Json String, I want my View to produce on HttpPost Request :
{
"product":
{
"title": "BiCycle",
"body_html": "Body of Battery",
"vendor": "Burton",
"product_type": "Product",
"status": "active",
"variants":
[
{
"title": "Default Title",
"price": "500.00",
"grams": 45,
"weight": 22.0,
"inventory_quantity": 23,
}
]
}
}