Firstly, I looked through all the messages that I could find, handling this error when calling JQuery Ajax, but most of them, due to incorrect data, were expected, and I'm sure that is not my problem, as this code worked before.
What I'm trying to do is when I click on one of my checkboxes, I want to get a partial view with the model and populate it in the #selectedProductContainer on my main view.
This is my main view (slightly shortened) with the scripts being executed:
@model ProductMainViewModel
<div class="product-list-wrapper">
@Html.CheckBoxListFor(m => m.PostedProducts.ProductIds,
m => m.AvalibleProducts,
entity => entity.Id,
entity => entity.Name,
m => m.SelectedProducts,
m => new { @class = "productCheckboxList productSelected" + m.Id, data_price = m.Price.ToString() })
</div>
<div id="selectedProductContainer">
</div>
<script>
$(".productCheckboxList[type='checkbox']").change(function () {
var isChecked = $(this).is(":checked");
var value = $(this).val();
if (isChecked) {
ProductChange(this);
}
function ProductChange(element) {
var value = {
Value: element.value
}
var container = $("#selectedProductContainer");
$.ajax({
url: "@Url.Action("ProductCalcSelection", "Home")",
type: "POST",
data: JSON.stringify(value),
contentType: "application/json",
dataType: "html",
success: function(data) {
container.append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("error: " + errorThrown);
}
});
}
<script>
The controller action for this call is as follows:
[HttpPost]
public ActionResult ProductCalcSelection(string value)
{
var convertValue = Convert.ToInt32(value);
var product = db.Products.FirstOrDefault(p => p.Id == convertValue);
var accessories = db.Accessories.ToList();
var productViewModel = new ProductFormViewModel
{
Product = product,
Accessories = accessories
};
return PartialView("_ProductForm", productViewModel);
Here is my first model for my partial view:
public class ProductFormViewModel
{
public Product Product { get; set; }
public List<Accessory> Accessories { get; set; }
}
And here is my partial view (short version):
@model ProductFormViewModel
<div>
<span>Hello</span>
</div>
, ajax- ": " .
, . , , .
, , , html , , ?
, Accessory, , , .
, :
public class Product : IProduct
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public bool HasAccessories { get; set; }
}
public class Accessory : IAccessory
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string CodeName { get; set; }
public string AccessoryType { get; set; }
public long Price { get; set; }
public bool IsMultiplied { get; set; }
public int ProductId { get; set; }
}
, Accessory, , , , ICollection . :
public class ProductId
{
[Key]
public int Id { get; set; }
public int ProductIdHolder { get; set; }
[ForeignKey("Accessory")]
public int AccessoryId { get; set; }
public virtual Accessory Accessory { get; set; }
}
, int ProductId.
, ?
EDIT: devotools :
POST http://localhost:54213/Home/ProductCalcSelection 500 ( )
@jquery-2.1.3.js: 8625
jQuery.extend.ajax @jquery-2.1.3.js: 8161
ProductChange @offert.js: 113
( ) @Offert: 179
jQuery.event.dispatch @jquery-2.1.3.js: 4430
elemData.handle @jquery-2.1.3.js: 4116
113 offer .js $.ajax.
179 ProductChange().