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

ASP.NET MVC生成Create视图时,嵌套类属性为何未生成HTML字段?

问题原因

Audit类缺少无参构造函数,ASP.NET MVC的视图脚手架(Scaffolding)在生成视图时,需要模型类具备无参构造函数才能识别并生成嵌套类的属性字段。当类仅包含带参数的构造函数时,脚手架无法实例化该类,因此不会为其属性生成对应的HTML输入控件。另外,原Audit类构造函数中的Datetime是拼写错误,应为DateTime,这会导致编译失败,也会影响脚手架的正常工作。

解决方法

1. 为Audit类添加无参构造函数并修正拼写错误

修改Audit类代码,添加无参构造函数并修正日期类型的拼写错误:

public class Audit
{
    public string Requestor { get; set; }
    public string Approver { get; set; }
    public DateTime RequestedOn { get; set; }
    public DateTime ApprovedOn { get; set; }

    // 添加无参构造函数
    public Audit()
    {
    }

    public Audit(string requestor, string approver, DateTime requestedOn, DateTime approvedOn)
    {
        Requestor = requestor;
        Approver = approver;
        RequestedOn = requestedOn;
        ApprovedOn = approvedOn;
    }
}

修改完成后,重新生成Create视图,脚手架会自动为ObjAudit的所有属性生成对应的HTML字段。

2. 手动在现有视图中添加Audit属性的字段

如果不想重新生成视图,可以手动在表单中插入ObjAudit各属性的输入控件,示例代码如下:

<div class="form-group">
    @Html.LabelFor(model => model.ObjAudit.Requestor, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.ObjAudit.Requestor, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.ObjAudit.Requestor, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.ObjAudit.Approver, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.ObjAudit.Approver, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.ObjAudit.Approver, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.ObjAudit.RequestedOn, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.ObjAudit.RequestedOn, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.ObjAudit.RequestedOn, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.ObjAudit.ApprovedOn, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.ObjAudit.ApprovedOn, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.ObjAudit.ApprovedOn, "", new { @class = "text-danger" })
    </div>
</div>

将这段代码插入到ClaimNumbers字段的div之后、提交按钮的div之前即可。

额外注意事项

在控制器的Create GET方法中初始化ObjAudit,避免视图渲染时出现空引用异常:

public ActionResult Create()
{
    var model = new UpdateClaim
    {
        ObjAudit = new Audit()
    };
    return View(model);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 00:07:10