编辑视图未填充对象数据

时间:2017-05-11 09:02:37

标签: razor asp.net-core-mvc asp.net-core-1.1

我是ASP .Net的新手,可以使用一些帮助......我有一个ASP .Net Core 1.1网络应用程序。在其中,我有一个“编辑”视图,用于编辑一个简单的对象,相应的控制器在路由到它时会调用它。这是观点:

@model InspectionsTestClient.Models.Property

@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

@Html.ValidationSummary();

<form asp-action="Edit">
    <div class="form-horizontal">
        <h4>Property</h4>
        <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>
        </div>
        <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>
        </div>
        <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>
            </div>
        </div>
    </div>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

这是调用该视图的控制器:

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

这是模特:

namespace InspectionsTestClient.Models
{
    //[Table("property")]
    public class Property
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        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; }
    }

}

因此,当我导航到该页面时,控制器将启动,并返回“编辑”视图。我已经确认填充了参数“id”。但是,当编辑视图在浏览器中加载时,所有输入文本框都为空。我希望它们预先填充了相关对象的值。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您遇到的问题正在发生,因为您没有将该对象返回到视图中..实际上,在您的情况下,您甚至没有去数据库获取对象。

您需要将Edit动作修改为以下内容:

// GET: Property/Edit/5
public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var object = db.TableName.Find(id);

    // db = connectionstring
    // TableName = database table that holds the object that you want to return

    if (object == null)
    {
        return HttpNotFound();
    }

    return View(object);
}

如果有帮助,请告诉我