c# - 如何从 mvc 4 razor 中的共享 View 访问 Controller 操作?

标签 c# asp.net-mvc razor asp.net-mvc-4

我已经创建了一个 View 索引,并且在该 View 上我已经在该 View 上放置了一个共享 View ,但现在我必须从该共享 View (_TraningList)调用 Controller 操作

这是索引 View

为此我做了以下事情

 @Html.Partial("~/Views/Shared/_TrainingList.cshtml", Model.First().Categories)
 @Html.Partial("_ConsultingList")
 @Html.Partial("_UpcomingWebinarList")

这是共享 View

@model IEnumerable<QPS_MVC.BusinessLogic.CourseCategories>
<!--Training Box-->
<div class="bodyContentWrp">
@*@Html.Action("Index", "Trainings")*@
<h1>
    Training Programs</h1>
@*  <ul class="arw1">
            <li>Aerospace</li>
            <li>Agile</li>
            <li>APICS/SME Certifications</li>
            <li>ASQ Certifications</li>
            <li>FDA Related</li>
            <li>HR Related</li>
            <li>ISO Related</li>
            <li>IT Certifications</li>
            <li>Lean Six Sigma</li>
            <li>PMI Certifications</li>
        </ul>
*@
@foreach (QPS_MVC.BusinessLogic.CourseCategories item in Model)
{
    <ul>
        <li>@Html.ActionLink(item.Name, "", "Trainings", new { SelectedId = item.CategoryID },   null)</li>
        <li></li>
    </ul>
}
<div class="clear">
</div>
<div class="readMore">
    <a href="#">Read More</a></div>
<div class="clear">
</div>

这是 Controller

 public ActionResult Index()
    {
        List<Courses> obj = new List<Courses>();

        if (Request.QueryString.Count > 0 && Request.QueryString["SelectedId"] != null && !string.IsNullOrEmpty(Request.QueryString["SelectedId"].ToString()))
        {

            obj = new CoursesApp().getAllCoursesById(int.Parse(Request.QueryString["SelectedId"].ToString()));

        }
        else
        {
            obj = new CoursesApp().getAllCourses();
            obj = new CoursesApp().getAllCoursesById(obj.First().Categories.First().CategoryID);
        }
        //if (ViewData["CategoryId"] == null || string.IsNullOrEmpty(ViewData["CategoryId"].ToString()))
        //{
        //    obj = new CoursesApp().getAllCourses();
        //    obj = new CoursesApp().getAllCoursesById(obj.First().Categories.First().CategoryID);
        //}
        //else
        //{
        //    obj = new CoursesApp().getAllCoursesById(int.Parse(ViewData["CategoryId"].ToString()));
        //}
        return View(obj);
    }

所以基本上我想从共享 View _trainingList调用这个索引操作 我已经完成了上述事情,但我无法成功,所以请任何人帮助我。

最佳答案

您可以使用RenderAction

Invokes a child action method and renders the result inline in the parent view.

@{ Html.RenderAction("Action", "Controller"); }

关于c# - 如何从 mvc 4 razor 中的共享 View 访问 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15522798/

相关文章:

c# - 如何使用 Dapper-dot-net 从单个 SP 映射多个记录

c# - Axis equal in C# chart(How to make equal axes in C# Chart control)

asp.net-mvc - 在 Web API 中执行操作后更改返回的对象值

javascript - 在 asp.net 中捆绑

c# - 让单元测试返回 async void 不好吗?

c# - 这可以用最小起订量来模拟吗?

asp.net-mvc - ASP.Net MVC 和 Webservices/Ajax - 走哪条路?

razor - ASP.NET Core - 像 MVC 一样的脚手架标识

c# - View 是否可以在不先将其保存到文件的情况下显示 WebImage?

asp.net - "@Url.Content"和 "@Href"有什么区别?