传递给控制器​​的锚标记的文本

时间:2014-03-06 08:05:02

标签: c# html asp.net-mvc

是否可以将锚标记的文本传递给控制器​​?

<a href="@Url.Action("SearchStudent","Home")">Student Information</a>

这是在视图中。

当我点击链接时,我想在控制器中使用“学生信息”(内部文本)。

如何以编程方式获取它? (无硬编码)

我正在使用C#。

先谢谢。

2 个答案:

答案 0 :(得分:1)

您可以将其作为routeValues传递。

在您看来:

<a href="@Url.Action("SearchStudent","Home", new { innerText = "Student Information" })">Student Information</a>

在您的控制器中:

public ActionResult SearchStudent(string innerText)
{
   // Do your thing.
   return View();
}

答案 1 :(得分:0)

如果你真的想将内部文本发送到控制器

,我有一个例子

像这样给你的标签id

<a id="linkData">Student Information</a>

在jQuery中添加此

$('#linkData').click(function () {
            var dataLink = $('#linkData').text();
            $.ajax({
                type: "POST", data: { data: dataLink }, url: "@Url.Action("SearchStudent","Home")", success: function (result)
                {
                    alert(result);
                }
            });
        });

在你的控制器中添加一个这样的参数

    public ActionResult SearchStudent(string data)
    {
        return  Json(data, JsonRequestBehavior.AllowGet);
    }

它将数据返回到函数(结果)和警报(结果)以显示您的内部文本。 希望你能理解我。我的英语不好。

相关问题