简体   繁体   English

如何访问从 MVC 视图中的 ajax 调用调用的控制器返回的数据

[英]How do I access data returned from a controller invoked by an ajax call in an MVC View

I have an MVC 5 view with the following JavaScript which is getting an error after invoking an action method on a controller:我有一个带有以下 JavaScript 的 MVC 5 视图,在调用控制器上的操作方法后出现错误:

<script type="text/javascript">
    $('#ddlvendors').change(function () {
                var descHtml = "";
                var vendorId = $(this).val(); 

                $.ajax(
                    {
                        type: "POST",
                        data: { vendorId: vendorId },
                        url: '@Url.Action("PurchaseOrderVendor")',
                        datatype: "json",
                        success: function (aVendorObject) {
                            alert(aVendorObject.Name);
                        },
                        error: function (req, status, error) {
                            alert(error);
                        }
                     });
            });
</script>

The controller action method is as follows:控制器动作方法如下:

[HttpPost]
public ActionResult PurchaseOrderVendor( int vendorId)
{
  Vendor aVendor=VendorServices.GetVendor(vendorId);

  return Json(aVendor);
}

The vendor object being returned is as follows:返回的供应商对象如下:

 public class VendorViewModel
  {
    public int VendorId { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Description { get; set; }
    public string ContactName { get; set; }
    public string Phone { get; set; }
  }

Using the Visual Studio 2017 debugger I can see the vendor data is correctly getting filled on the controller side.使用 Visual Studio 2017 调试器,我可以看到供应商数据在控制器端正确填充。 If I don't try to access any member data the ajax call completes without an error.如果我不尝试访问任何成员数据,ajax 调用就会完成而不会出错。 However, when I try to access any part coming in as aVendorObject such as aVendorObject.Name I get a browser error saying "Internal Server Error".但是,当我尝试访问作为 aVendorObject 进入的任何部分(例如 aVendorObject.Name)时,我收到一个浏览器错误,提示“内部服务器错误”。 How can I fix my success handler in the ajax call so I can access members of the Vendor object being returned from the controller?如何在 ajax 调用中修复我的成功处理程序,以便我可以访问从控制器返回的 Vendor 对象的成员?

Thanks in advance.提前致谢。

The LazyLoading feature of EntityFramework 6 did not have the Vendor object available when it was needed. EntityFramework 6 的 LazyLoading 功能在需要时没有提供 Vendor 对象。 I added a line in the VendorServices class to turn off LazyLoading which fixed the problem.我在 VendorServices 类中添加了一行以关闭解决问题的 LazyLoading。 Below is the method where I turn off LazyLoading for this particular VendorService method:下面是我为这个特定的 VendorService 方法关闭 LazyLoading 的方法:

 static public Vendor GetVendor(int aVendorId)
{
  Vendor vendor;
  using (RgmSiteDAL RgmSiteDALCtx = new RgmSiteDAL())
  {
    //** Now setting LazyLoadingEnabled to false.
    RgmSiteDALCtx.Configuration.LazyLoadingEnabled = false;

    vendor = RgmSiteDALCtx.Vendor
                .Where(v => v.VendorId == aVendorId)
               .FirstOrDefault<Vendor>();

  }
    return vendor;
}

I found out that I can also fix the problem if I include any navigation properties defined with the Vendor entity as in the following:我发现如果我包含使用 Vendor 实体定义的任何导航属性,我也可以解决这个问题,如下所示:

vendor = RgmSiteDALCtx.Vendor
                   .Where(v => v.VendorId == aVendorId)
                   .Include(v => v.PurchaseOrder) //because is in Vendor navigation property
                  .FirstOrDefault<Vendor>();

For my situation I will stick with the first solution which sets LazyLoadingEnabled to false.对于我的情况,我将坚持使用第一个将 LazyLoadingEnabled 设置为 false 的解决方案。 This is because the option to generate my entities from my existing database setup my PurchaseOrder table as a navigation property for my Vendor which I believe is incorrect.这是因为从我现有的数据库生成我的实体的选项将我的 PurchaseOrder 表设置为我的供应商的导航属性,我认为这是不正确的。 My Vendor object does not need anything from my PurchaseOrder entity when I query for Vendor information.当我查询供应商信息时,我的供应商对象不需要来自 PurchaseOrder 实体的任何内容。 I will look into removing the PurchaseOrder navigation property from my Vendor entity because I believe it was incorrectly setup by the create entities from database tool in Visual Studio.我将研究从我的供应商实体中删除 PurchaseOrder 导航属性,因为我认为它是由 Visual Studio 中的数据库工具创建实体错误设置的。

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

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