简体   繁体   English

子对象在Ajax Post之后返​​回Null

[英]Child Object returns Null after Ajax Post

I am submitting some info using an Ajax post which is supposed to refresh a partial view. 我正在使用应该刷新部分视图的Ajax帖子提交一些信息。 The info gets sent to the database however on return of the partial view, the Fees.Feetype data can't be rendered as it is null. 信息将发送到数据库,但是在返回部分视图时,Fees.Feetype数据为空,因此无法呈现。

On manual refresh, the Fees.FeeType is rendered with no problem. 手动刷新时,Fees.FeeType呈现没有问题。

How do I get the FeeType to be included in the partial view after the ajax post? 如何在ajax发布后将FeeType包含在部分视图中?

Models 楷模

    public class Members
    {
        public virtual ICollection<Fees> Fees { get; set; }
    }
    public class Fees
    {
            public int FeeTypesId { get; set; }
            public virtual FeeTypes FeeType { get; set; }
            public virtual Members Members { get; set; }
    }
    public class FeeTypes
    {
        public int Id { get; set; }
        public string FeeTypeName { get; set; }
    }

Ajax Post 阿贾克斯邮报

            var token = $('[name=__RequestVerificationToken]').val();
            var postData = {
                __RequestVerificationToken: token,
                FeeOccurence: feeOccField.val(),
                FeeAmount: amountField.val(),
                FeeStatus: statusField.val(),
                FeeTypesId: typeField.val(),
                FeeDate: dateField.val(),
                MemberNo: memberNo
            };
            $.ajax({
                url: '/Members/AddFees',
                type: 'POST',
                data: postData,
                success: function (members) {
                    alert(result);
                    $("#details").html(members);
                },
                error: function (result) {
                    alert(result);
                },
                traditional: true

Controller 控制者

        public ActionResult AddFees (Fees fees)
        {
            if (ModelState.IsValid)
            {

                db.Fees.Add(fees);

                db.SaveChanges();

                Members members = new Members();
                members = db.Members.Find(fees.MemberNo);

                return PartialView("~/Views/Members/_MemberDetails.cshtml", members);
            }
        }

Views 观看次数

@model AccPortal.Models.Members
    <div class="col-md-7 md-fees" id ="details">
        @Html.Partial("_MemberDetails", Model)
    </div>
    //This is the partial view.    
        @model AccPortal.Models.Members
            @foreach (var item in Model.Fees)
            {
//The error happens on this line but all other Fees properties are rendered without issue.
                <td class="md-fee-value"data-type="status">@item.FeeType.FeeTypeName</td>
            }

These are the things you need to change in your code, 这些是您需要在代码中进行更改的内容,

First, return a partial view with model from your Action method like this, 首先,从这样的Action方法返回带有模型的局部视图,

return PartialView("_MemberDetails", members);

Datatype should be html in your ajax method. 在您的ajax方法中, Datatype应为html。 So add this bellow line inside ajax method. 因此,在ajax方法内添加此波纹管行。 You can add this line after this line data: postData, 您可以在以下行data: postData,之后添加此行data: postData,

dataType: "html",

Now in your ajax success method, change like this, 现在,在您的ajax成功方法中,进行如下更改,

success: function (members) {
             //alert(result);  what is result here, that you have written
             $("#details").html(''); //first make it blank then render
             $("#details").html(members);
            },

In your partial view, you need to check whether FeeType is there or not before render the value, 在局部视图中,您需要先检查FeeType是否存在,然后再呈现该值,

@model AccPortal.Models.Members
@foreach (var item in Model.Fees)
 {  
  if(item.FeeType != null)
   <td class="md-fee-value"data-type="status">@item.FeeType.FeeTypeName</td>
  else
   <td class="md-fee-value"data-type="status"></td> //You can write NA here
 }

The issue with the null type was lazy loading in the end. null类型的问题最后是延迟加载。

I had to requery the db after I had saved the changes and include Fees.FeeTypes to return the correctly populated model. 保存更改并包含Fees.FeeTypes以返回正确填充的模型后,我不得不重新查询数据库。

        members = db.Members.Find(fees.MemberNo);
        members.Fees = db.Fees.Include(x => x.FeeType).Where(x => x.Members.MemberNo.Equals(x.MemberNo)).ToList();

you need to check for null values before populating in the list to avoid null exception 您需要在填充列表之前检查null值,以避免出现null exception

 @model AccPortal.Models.Members
            @foreach (var item in Model.Fees)
            {
              if(item.FeeType != null)
                <td class="md-fee-value"data-type="status">@item.FeeType.FeeTypeName</td>
            }

because some data might be missing which is causing problem so with if condition it will populated only that data which is not null 因为可能会丢失一些数据,这会导致问题,所以if条件满足,它将仅填充not null数据

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

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