在局部视图中指定模型会影响整个剃刀体还是仅影响局部视图区域?

时间:2016-03-25 10:31:03

标签: c# asp.net-mvc razor

这是我的部分观点

@model MyModel<br>
@using (Html.BeginForm("NewOrder", "Order", FormMethod.Post))
{
    <div class="form-inline">
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerMessage, htmlAttributes: new { @class = "control-label col-md-1" })
            <div class="col-md-2">
                @Html.EditorFor(model => model.CustomerMessage, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-1" })
            <div class="col-md-2">
                @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>
    </div>
}

现在如果我在一个视图中使用它,这个效果我的整个页面或只有我渲染这个局部视图的部分会受到影响吗?

1 个答案:

答案 0 :(得分:0)

  

现在,如果我在视图中使用它,这将影响我的整个页面或仅影响   我已经渲染了这个局部视图的部分会受到影响吗?

不,它不会影响整个页面。它只会影响您渲染此局部视图的部分。实际上这是部分观点的好处之一。您可以在这里和那里渲染小块html,而不会影响它们所包含的整个页面。

  

如何将模型传递给局部视图?

你可以这样做:

old end date = 2016-03-24 23:59:59 +0000
2016-03-26 10:20:22 +0000 is not within 2016-03-24 00:00:00 +0000 & 2016-03-24 23:59:59 +0000
old start date = 2016-03-24 00:00:00 +0000
new StartDate = 2016-03-25 00:00:00 +0000
Start: 2016-03-25 00:00:00 +0000 End: 2016-03-25 23:59:59 +0000
2016-03-26 10:20:22 +0000 is not within 2016-03-25 00:00:00 +0000 & 2016-03-25 23:59:59 +0000
old start date = 2016-03-25 00:00:00 +0000
new StartDate = 2016-03-25 00:00:00 +0000

我建议您在视图的 @Html.Partial("~/Views/Shared/_Partial.cshtml", Model.PartialViewModel) 中定义一个与要传递给局部视图的模型相关联的属性。这是有道理的,因为为了渲染视图,您还必须渲染包含在其中的部分视图。因此,将部分视图所需的所有模型作为视图模型的属性是一种很好的做法。