返回部分视图时包含对象

时间:2017-04-09 09:12:44

标签: c# jquery .net ajax asp.net-mvc

我试图让部分视图在迭代集合时显示对象名称。当我刷新原始页面时没有问题,但是当我做一个AJAX帖子并尝试将新对象返回到局部视图时,它就找不到特定的对象。

我在FeeTypes对象上得到一个空对象错误,该对象应作为Members.Fees(whateverno)包含在members对象中.FeeTypesG

此外,ajax帖子不会刷新div成功,因为它永远不会获得成功(或者我做错了)注意:它仍然正确地将所有内容发布到数据库并保存正常。

模型

public class Members
{
    [Key]
    public int MemberNo { get; set; }

    [Display(Name = "Member Fees")]
    public virtual ICollection<Fees> Fees { get; set; }


}    




public class Fees
{
    [Key]
    public int FeeNo { get; set; }
    public virtual FeeTypes FeeType { get; set; }
    [Display(Name = "Fee Type")]
    public int FeeTypesId { get; set; }
    [Display(Name = "Frequency of Fee")]
    public FeeOccurence FeeOccurence { get; set; }
    [Display(Name = "Fee Amount")]
    public float FeeAmount { get; set; }
    [Display(Name = "Fee Status")]
    public FeeStatus FeeStatus { get; set; }
    [Display(Name = "Date received (for billing)")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? FeeDate { get; set; }
    public virtual Members Members { get; set; }
    public int MemberNo { get; set; }

}

public class FeeTypes
{
    public int Id { get; set; }
    [Display(Name = "Fee Name")]
    public string FeeTypeName { get; set; }



}

控制器操作

    public ActionResult AddFees(Fees fees)
    {
        string result;
        Members members = db.Members.Find(fees.MemberNo);
        ViewBag.Fees = db.Fees
        .Include(x => x.FeeType)
        .Where(x => x.Members.MemberNo.Equals(x.MemberNo));

        ViewBag.FeeTypesId = new SelectList(db.FeeTypes, "Id", "FeeTypeName");
        if (ModelState.IsValid)
        {

            db.Fees.Add(fees);

            db.SaveChanges();

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

        }

        ViewBag.MemberNo = new SelectList(db.Members, "MemberNo", "FirstName", fees.MemberNo);
        result = "Something went wrong!";
        return Json(result, JsonRequestBehavior.AllowGet);

    }

AJAX Post

        saveBtn.click(function () {
            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
            });

部分视图

    @using AccPortal.Models
    @model AccPortal.Models.Members

        <h5 class="md-fees-title">Fees</h5>
        <table cellpadding="0" cellspacing="0" class="md-fees-table">
            <thead>


                <tr>
                    <th>@Html.LabelFor(model => model.Fees.First().FeeDate)</th>
                    <th>@Html.LabelFor(model => model.Fees.First().FeeOccurence)</th>
                    <th>@Html.LabelFor(model => model.Fees.First().FeeTypesId)</th>
                    <th>@Html.LabelFor(model => model.Fees.First().FeeStatus)</th>
                    <th>@Html.LabelFor(model => model.Fees.First().FeeAmount)</th>
                    <th></th>
                </tr>

            </thead>
            <tbody>
                @{ int i = 0; }
                @foreach (var item in Model.Fees)
                {

                    <tr data-id="@i">
                        @Html.HiddenFor(model => model.Fees.ElementAt(i).FeeNo, new { @class = "md-fee-value", name = "md-fee-id", data_type = "testid", id = "testid" })
                        <td class="md-fee-value" data-type="date">@String.Format("{0:d}", item.FeeDate)</td>
                        <td class="md-fee-value" data-type="occ">@item.FeeOccurence.GetDisplayName()</td>
//ERROR HAPPENS ON THE NEXT LINE WHEN IT CANT FIND FEETYPES
                        <td class="md-fee-value" data-type="type">@item.FeeType.FeeTypeName</td>
                        <td class="md-fee-value" data-type="status">@item.FeeStatus</td>
                        <td class="md-fee-value" data-type="amount">@item.FeeAmount</td>
                        <td>
                            <button type="button" class="md-button md-button--edit" title="edit">
                                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="md-button md-button--remove" title="remove">
                                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                            </button>
                        </td>
                    </tr>
                    i++;
                }



            </tbody>

顶级视图

@using AccPortal.Models
@using Newtonsoft.Json
@model AccPortal.Models.Members

@{
    ViewBag.Title = "Details";
    @Styles.Render("~/Content/jqueryui")
}




<h2>Details</h2>

<div>
    <h4>Members Details</h4>
<hr />
</div>
<div class="row">

    <div class="col-md-7 md-fees" id ="details">

        @Html.Partial("_MemberDetails")
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

问题在于最终延迟加载。

我能够通过添加

让费用重新填充绑定的Members.Fees.FeeType
        members = db.Members.Find(fees.MemberNo);
        members.Fees = db.Fees.Include(x => x.FeeType).Where(x => x.Members.MemberNo.Equals(x.MemberNo)).ToList();