【问题标题】:Passing partial view with new model使用新模型传递局部视图
【发布时间】:2017-06-15 18:15:36
【问题描述】:

我在将单独的模型和部分视图传递到我的索引页面时遇到问题。下面我可以渲染部分视图,但它不会从我的控制器中点击方法“Opening”

局部视图

@model IList<DatabaseName.Models.Script>

@foreach(var item in Model)
{
    @item.ScriptName
}

索引

@model DatabaseName.Models.Lead

<div class="opening-fields">
@Html.Partial("~/Views/Scripts/_Opening.cshtml",List<DatabaseName.Models.StriptType())
</div>

在我的控制器中

public ActionResult Index(Guid? id)
{
    Lead lead = db.Leads.Find(id);
    blah blah blah;
    return View(lead);
}

public ActionResult Opening()
{
     var opening = from o in db.Scripts
                   where o.ScriptTypeID == 1
                   orderby o.ScriptOrder
                   select o;
    return PartialView("~/Views/Scripts/_Opening.cshtml", opening);
}

【问题讨论】:

  • @Html.Partial() 不调用服务器方法(它只是根据您传递的模型呈现其 html)。您需要 @Html.Action() 来调用服务器方法 - @Html.Action("Opening")

标签: .net asp.net-mvc razor


【解决方案1】:

您可以使用以下重载从视图中调用操作方法。

Public static MvcHtmlString Action(
    this HtmlHelper htmlHelper,
    string actionName,
    string controllerName,
    Object routeValues
)

https://msdn.microsoft.com/en-us/library/ee703457.aspx

您的索引示例:

@model DatabaseName.Models.Lead

<div class="opening-fields">
@Html.Action("Opening", "ControllerName", new { routevalue1="100", routevalue2="200" });  //changed line

</div>

希望对你有帮助

谢谢

卡提克

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多