2

I have a model class "UpdateClaim" with a nested class "Audit" in it. Both classes are in separate files.

When I generate a Create view from the "UpdateClaim" model, only the "ClaimNumbers" property under the "UpdateClaim" is creating the HTML field and the properties from the nested "Audit" class don't have an HTML field generated in the CSHTML. I have tried having the 2 classes in the same file, but it's the same result.

public class UpdateClaim
{
    [Display(Name = "Claim Numbers")]
    public string ClaimNumbers { get; set; }
    public Audit ObjAudit { get; set; }
}
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(string requestor, string approver, DateTime requestedOn, Datetime approvedOn)
    {
        Requestor = requestor;
        Approver = approver;
        RequestedOn = requestedOn;
        ApprovedOn = approvedOn;
    }
}

enter image description here

@model ProcessPro.Models.UpdateClaim

@{
    ViewBag.Title = "Trigger";
}

<h2>Trigger</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>UpdateClaim</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ClaimNumbers, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ClaimNumbers, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ClaimNumbers, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
prinkpan
  • 2,117
  • 1
  • 19
  • 32
Shinda
  • 21
  • 3
  • If that were to happen, where would you expect it to stop and how would the system know that? If you had a property of type `string`, why would what you want not cause a field to be generated for the `Length` property of the `string`, etc? – jmcilhinney Jun 01 '23 at 02:06
  • Appreciate your response. So you are suggesting that we can't really do this for nested classes and have to handle the nested class fields explicitly in the views? – Shinda Jun 02 '23 at 21:54
  • Correct. I don't know exactly what rules are used when scaffolding a view but I would guess that it is done for direct properties of the model that are of "simple" types. Really, you ought to have a different model specifically for the view. That way, you can flatten your data so everything is a direct property. Using the same model as you use for your data access isn't really ideal specifically because the two are likely to be different shapes. You can map data from your hierarchical data model to your flat view model, display, edit, then map back again and save. – jmcilhinney Jun 03 '23 at 02:41

0 Answers0