Edit view not being populated with objects data RRS feed

  • Question

  • Sorry, I know this is probably posted in the wrong section, but I couldn't find an ASP.Net section...

    I am quite new to ASP .Net, and could use some help... I have an ASP .Net Core 1.1 web app. In it, I have an "Edit" view for editing a simple object, which a corresponding controller calls when routed to it. This is the view:

    @model InspectionsTestClient.Models.Property
        ViewData["Title"] = "Edit";
    <form asp-action="Edit">
        <div class="form-horizontal">
            <hr />
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Id" />
            <div class="form-group">
                <label asp-for="UnitNumber" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="UnitNumber" class="form-control" />
                    <span asp-validation-for="UnitNumber" class="text-danger"></span>
            <div class="form-group">
                <label asp-for="BuildingName" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="BuildingName" class="form-control" />
                    <span asp-validation-for="BuildingName" class="text-danger"></span>
            <div class="form-group">
                <label asp-for="Street" class="col-md-2 control-label"></label>
                <div class="col-md-10">
                    <input asp-for="Street" class="form-control" />
                    <span asp-validation-for="Street" class="text-danger"></span>
        <a asp-action="Index">Back to List</a>
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    This is the controller which calls that view:

    // GET: Property/Edit/5
            public ActionResult Edit(int id)
                return View();

    And this is the model:

    namespace InspectionsTestClient.Models
        public class Property
            public int? Id { get; set; }
            [MaxLength(10, ErrorMessage = "Unit number too long")]
            [Display(Name = "Unit #")]
            public string UnitNumber { get; set; }
            [MaxLength(45, ErrorMessage = "BuildingName name too long")]
            public string BuildingName { get; set; }
            [MaxLength(45, ErrorMessage = "Street too long")]
            public string Street { get; set; }

    So when I navigate to that page, the controller fires up, and returns the Edit view. I have confirmed the parameter "id" is populated. When the Edit view loads in the browser, however, all the input textboxes are empty. I would expect them to be pre-populated with the values for the object in question. What am I missing?

    Fabricio Rodriguez - Pretoria, South Africa

    • Moved by CoolDadTx Thursday, May 11, 2017 3:35 PM ASP.NET related
    Thursday, May 11, 2017 2:48 PM


All replies