You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC:预填充模型时Ajax.BeginForm内分部视图不渲染集合

问题:预填充模型时分部视图无法渲染集合内容

使用Ajax.BeginForm实现向分部视图持续添加集合的功能,其余场景均正常,但传入预填充模型时,分部视图中的循环无法渲染集合。已确认该集合确实包含数据。

BusinessOwner.cshtml 代码

@model CallCenterWebsiteHelper.Models.BusinessOwner
@{
    ViewBag.Title = "BusinessOwner";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
}
<hgroup class="title">
    <h2>Business OwnersA</h2>
</hgroup>
<div style="width: 600px; border: 1px solid silver" id="OwnerEntry">
    @using (Ajax.BeginForm("AddBusinessOwner", "LoanApplication", new AjaxOptions { UpdateTargetId = "AllBusinessOwners", HttpMethod = "Post" }))
    {
        <div style="width: 600px; border-width: 1px">
            @Html.LabelFor(m => m.OwnerModel.OwnerName)
            @Html.TextBoxFor(m => m.OwnerModel.OwnerName, new { maxlength = 200 })
            @Html.ValidationMessageFor(m => m.OwnerModel.OwnerName)
            <br />
            <input type="submit" value="Add" style="width: 70px; font-size: 12px" />
        </div>
        <div id="AllBusinessOwners">
            @{Html.RenderPartial("_BusinessOwnerList", Model);}
        </div>

    }
</div>
@using (Html.BeginForm("MoveToFinanceAdvance", "LoanApplication", FormMethod.Post))
{
    <input type="submit" name="btnBack" value="Back" class="btn btn-default" />
    <input type="submit" name="BtnNext" value="Next" class="btn btn-default" />
    @ViewBag.MissingOwners
}

_BusinessOwnerList.cshtml 代码

@model CallCenterWebsiteHelper.Models.BusinessOwner

<div style="height: 300px; width: 600px; overflow-y: auto">
    <table border="1">
        @if (Model != null && Model.OwnerModel != null)
        {
            for (int i = 0; i < Model.OwnerModels.Count; i++)
            {

                <tr style="vertical-align: top">
                    <td style="font-size: 12px; padding-left: 2px">
                        @Html.DisplayFor(modelItem => Model.OwnerModels[i].OwnerName)
                    </td>
                </tr>

                @Html.HiddenFor(modelItem => Model.OwnerModels[i].OwnerName)
            }
        }
    </table>
</div>

后台代码

public ActionResult AddBusinessInformation(Reverberate.BLL.Model.Form.Application.BusinessInfoModel viewModel, string btnReset, string BtnNext)
{
    applicationForm form = GetApplicationForm();

    if (btnReset != null)
    {
        RemoveApplication();
        return View("BusinessInfo", new Reverberate.BLL.Model.Form.Application.BusinessInfoModel());
    }

    if (!ModelState.IsValid)
    {
        return View("BusinessInfo", viewModel);
    }

    if (BtnNext != null)
    {
        if (ModelState.IsValid)
        {
            form.BusinessInfo = viewModel;

            if (form.ApplicationOwnerInformationModels.Count > 0)
            {
                var model = new CallCenterWebsiteHelper.Models.BusinessOwner();
                model.OwnerModels = form.ApplicationOwnerInformationModels;
                return View("BusinessOwner", model);
            }
            else
                return View("BusinessOwner");
        }
    }

    return View();
}

问题原因及解决办法

问题出在分部视图_BusinessOwnerList.cshtml的判断条件上:当前判断逻辑是Model != null && Model.OwnerModel != null,但预填充模型时,后台仅为OwnerModels集合赋值了数据,OwnerModel字段为空,导致判断条件不成立,循环代码未执行。

修改分部视图中的判断条件,改为直接检查目标集合的有效性:

@if (Model != null && Model.OwnerModels != null && Model.OwnerModels.Count > 0)

调整后,即使OwnerModel为空,只要OwnerModels集合存在数据,就能正常渲染循环内容。

内容的提问来源于stack exchange,提问作者user1257758

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 01:28:20