首页 > 解决方案 > 如何通过锚标签调用带有任何 MVC5 控制器参数的索引视图?

问题描述

我有一个 Mvc 控制器

public class ApplicationsController : Controller
    {
        // GET: Applicants/Applications
        public ActionResult Index(Guid? ApplicationId)
        {
           //some work with the ApplicationId
            return View("Application Details");
        }


    }

在我的另一个 HTML 页面中有一个锚标记,我想通过它调用这个控制器的默认视图(索引视图)。网址应为:

http://localhost:66666/Applicants/Applications/xxxxxxxxxxxxxxxxxxxxxxxxx

我正在尝试通过锚标签调用它:

 <script id="applicationDetailstemplate" type="text/x-jsrender">
<a href="../../../Applicants/Applications/{{:id}})">{{:position}}</a>
 </script>

网址正确,但从未调用该操作。如果我从锚标记中删除 id,控制器视图会被调用,但 ApplicationId 为空。我怎样才能传递 id 呢?

标签: asp.net-mvcanchor

解决方案


我怀疑,除非您更改了默认路由规则,否则您需要将其编写为

../../../Applicants/Applications?ApplicationId={{:id}}

通常,只有当您的操作方法签名中的变量实际上只是“id”(不是任何其他名称)时,您将 ID 放入路径(而不是查询字符串)的形式才有效。

此外,如果这是视图的一部分,请考虑使用 HTML 帮助程序以确保始终为路径的主要部分生成正确的 URL,例如:

@Url.Action("Applicants", "Applications")?ApplicationId={{:id}}

推荐阅读