500 - jQuery的阿贾克斯后内部服务器错误(ASP.NET MVC)错误、服务器、阿贾克斯后、jQuery

2023-09-10 20:22:40 作者:三分温柔七分傲娇

首先,我一直在寻找通过所有帖子我能找到处理此错误的jQuery的Ajax调用,但其中大部分是由于预期的回错了数据类型,我就是pretty的肯定那不是我的因为这code问题之前工作过。

First off, I've been looking through all posts I could find that handles this error on JQuery Ajax calls, but most of them as due to wrong dataTypes expected back, and I'm pretty sure that's not my problem since this code has worked before.

当我点击我的复选框之一,我想要检索与模型的局部视图,并填充它#selectedProductContainer我的主视图我想要做的是。

What I'm trying to do is when I click one of my checkboxes, I want to retrieve a partial view with a model and populate it in #selectedProductContainer on my main view.

这是我的主要观点(缩短了不少),与正在运行的脚本:

This is my main view (Shortened quite a bit) with the scripts that's running:

@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 looks like this:

[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 first my View 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-叫我回来的错误:内部服务器错误在我的错误处理的警告框。 我通过这个整个呼叫调试,以及所有我的价值观是正确的。它似乎发疯当控制器发送回的部分观点和我的观点模型。

The issue is that from the ajax-call I get back "error: Internal Server Error" in my alert box for the error handling. I've debugged through this entire call, and all my values are correct. It seems to freak out when the controller sends back the partial view and my view model.

在我的内心深处我觉得这是由于错误的dataType作为回应,但我接受HTML回来了,这是我应该得到的,对不对? 这个工作较早,但我们不得不做出一些小改变附件型号,我知道,这个问题应该连接到这种变化,但我似乎无法找到连接。 我会后我的班也在这里,如果他们需要将:

In the back of my mind I feel like this is due to wrong dataType as response, but I expect html back, and that's what I should get, right? This worked earlier, but we had to make some small changes to the Accessory model and I'm aware that the problem should be connected to this change, but I can't seem to find the connection. I'll post my classes here too if they would be needed:

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; }
    //public virtual ICollection<ProductId> ProductIds { get; set; }
}

现对附件的变​​化,我们决定,我们只需要每个附件1的ProductId,前面我们使用虚拟ICollection的存储多个标识的。这个类是这样的:

The change we made on Accessory was that we decided that we only need one ProductId per Accessory, earlier we use the virtual ICollection to store multiple Id's. That class looked like this:

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。

We're not using this at all now, but instead uses the regular int ProductId.

任何想法,以什么可能出了错?

Any ideas to what could have gone wrong?

编辑:我将我的浏览器devtools错误消息:

I'm adding my browser devtools error message:

POST 的http://本地主机:54213 /主页/ 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

POST http://localhost:54213/Home/ProductCalcSelection 500 (Internal Server Error) send @ jquery-2.1.3.js:8625 jQuery.extend.ajax @ jquery-2.1.3.js:8161 ProductChange @ offert.js:113 (anonymous function) @ Offert:179 jQuery.event.dispatch @ jquery-2.1.3.js:4430 elemData.handle @ jquery-2.1.3.js:4116

线113 offert.js是我的$就开始的地方。 线179对匿名函数是我的电话,以ProductChange()函数。

Line 113 in offert.js is where my $.ajax begins. Line 179 for anonymous function is my call to ProductChange() function.

推荐答案

随着@Mackan和@gamesmad指出,在评论中,我一直在寻找在错误的错误。当我在devtools打开网络和点击呼叫 - > preVIEW我,给了我所需要的所有信息,一个asp.net错误消息。在我来说,我忘了更新局部视图我送回来更新的模型。

As @Mackan and @gamesmad pointed out in the comments, I was looking at the wrong error. When I opened Network in devtools and clicked the call->Preview I got an asp.net error message that gave me all information I needed. In my case I had forgotten to update the partial view I send back with the updated model.