【问题标题】:@Html.Partial Throws CS1513: } expected Error@Html.Partial 引发 CS1513:} 预期错误
【发布时间】:2015-07-24 09:54:05
【问题描述】:

我研究了有关错误CS1513: } expected 的多个其他问题,但没有一个有帮助。

我正在尝试将部分视图引入 C# MVC 5.2 中的视图。

我正在使用剃刀语法@Html.Partial("MyPartialView"),也尝试过@{ Html.RenderPartial("MyPartialView"); },但无济于事。

执行代码时,我在包含渲染助手的视图中收到错误消息,该错误位于我试图包含部分视图的行上。

如果我将部分视图粘贴到主视图中,则不会发生错误,这让我感到困惑,因为肯定没有缺少花括号。

以下是代码示例:

主视图

@model ProjectBagel.Models.BagelMania

@using(Html.BeginForm("SomeAction", "SomeController", FormMethod.Post))
{
    <fieldset>
        @Html.LabelFor(model => model.NormalBagel, new { @class = "control-label" })
        @Html.EditorFor(model => model.NormalBagel, new { htmlAttributes = new { @class = "form-control" }, })

        @Html.Partial("EvenTastierBagels") //Error here - MVC doesn't like a tastier bagels :-(

        @Html.LabelFor(model => model.LessTastierBagel, new { @class = "control-label" })
        @Html.EditorFor(model => model.LessTastierBagel, new { htmlAttributes = new { @class = "form-control" }, })

    </fieldset>
}

局部视图

@model ProjectBagel.Models.SuperAwesomeBagelMania

    @Html.LabelFor(model => model.SuperAwesomeBagel, new { @class = "control-label" })
    @Html.EditorFor(model => model.SuperAwesomeBagel, new { htmlAttributes = new { @class = "form-control" }, })

谁能提出问题可能是什么?


免责声明:我既不销售也不支持百吉饼。

【问题讨论】:

  • @model = ProjectBagel... 应该是 @model ProjectBagel..(没有等号)
  • @StephenMuecke 抱歉,今天早上的百吉饼太多了...已经编辑了示例,因为这正好在项目中!
  • 我怀疑这是一个错字。我想那些 SuperAwesomeBagel 毕竟不是那么超级棒 :)
  • @{ Html.RenderPartial("_yourPartial"); } 你可能错过了分号
  • 您使用@Html.Partial("EvenTastierBagels") - 即只添加部分名称而不传递模型,这意味着默认情况下这应该将主模型的实例(BagelMania 的类型)传递给部分,但部分需要一个 typeof SuperAwesomeBagelMania 的模型。因此,您的代码实际上应该引发不同的异常。您确定您显示的代码正确吗?

标签: c# asp.net-mvc razor partial-views


【解决方案1】:

根据 cmets,我没有将视图模型传递给视图:

@Html.Partial("EvenTastierBagels", Model.BagelMania.SuperAwesomeBagelMania)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2016-03-02
    • 1970-01-01
    • 2021-05-24
    • 2022-07-28
    相关资源
    最近更新 更多