【问题标题】:MVC5 Partial ViewsMVC5 局部视图
【发布时间】: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


【解决方案1】:

首先为你的局部视图声明一个模型类型

@model MyNamespace.MyModel

由于您要将此视图与不同的模型重用,因此请为该视图分配一个基本类型或接口。

public interface IMyModel
{
    string AddressLine1 { get; set; }
    string City { get; set; }
    ...
}

然后是模型声明

@model MyNamespace.IMyModel

所以现在您可以使用具有可重用视图的不同模型

public class FooModel : IMyModel
{
    public int FooId { get; set; }

    // IMyModel properties
    public string Address1 { get; set; }
    public string City { get; set; }
}

及用法

@model MyNamespace.FooModel

@Html.Partial("_AddressParital", Model)

@Html.Partial("_AddressPartial", new FooModel { ... })

【讨论】:

  • 你的主视图是否声明了一个模型?
  • 是的,确实如此。我得到了你的解决方案,但是接口是一个新类,还是我可以在 PartialView 本身中声明它?
  • 在自己的文件中定义接口会更容易使用和查找。如果您正确定义了它的命名空间,那么在局部视图中或在实现具体模型时引用它应该不会有问题。
【解决方案2】:

只需在局部视图名称后传递模型即可。

@Html.Partial("_AddressPartial", model)

这将传递当前页面的模型,您可能只想传递其中的一部分,具体取决于您的模型,例如

@Html.Partial("_AddressPartial", model.Address)

注意:在您的局部视图中,您需要定义模型的类型...

@model MyNamespace.AddressModel

【讨论】:

  • 当我这样做时,我得到这个错误:“名称'模型'在当前上下文中不存在”我在视图页面的顶部有@model Example.ClassName
  • 尝试“模型”而不是“模型”,我可能弄错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2017-10-02
  • 1970-01-01
  • 2016-08-19
  • 2015-02-03
  • 2019-04-04
  • 2019-02-08
相关资源
最近更新 更多