【发布时间】:2015-12-14 19:56:11
【问题描述】:
我已经浏览了这里的示例,但我并不完全理解 MVC 中的视图和部分视图。
我有一个名为“Edit.cshtml”的视图,代码如下:
<div class="row">
<div class="form-group">
@Html.LabelFor(model => model.AddressLine1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.AddressLine1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AddressLine1, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.AddressLine2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.AddressLine2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AddressLine2, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.StateAbbreviation, "State", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownList("StateAbbreviation", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StateAbbreviation, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
</div>
@Html.LabelFor(model => model.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
</div>
</div>
由于此代码将用于多个模型,具有相同的格式,我想为它创建一个简单的局部视图。 _AddressPartial.cshtml 中的代码正是上面列出的内容。在视图中,我将其称为:
@Html.Partial("_AddressPartial")
它不起作用。我得到一个 HttpCompileException。我假设我需要以某种方式将我的模型对象发送给它,但我不确定那个模型是什么?视图包?我的问题是,我需要传递给它什么?
【问题讨论】:
-
编辑您的问题以包含
HttpComplieException的全文。
标签: asp.net-mvc-5 partial-views