如何引用Razor表达式中创建的控件?

时间:2018-04-05 23:07:04

标签: asp.net-mvc razor

我在MVC的View文件中有以下代码。我试图创建一个基本的搜索框功能。第一个控件是搜索框,第二个是我想要执行搜索的ActionLink。

ActionLink的第三个参数需要是搜索框的值,因此它可以将用户搜索的值传递给我的控制器 - 但我无法看到引用搜索的方法框。

如何参考此ActionLink参数中的搜索框值?



@using (Html.BeginForm())
{
    <p>
        Search First and/or Last Name: @Html.TextBox("SearchString") <br />
        @Html.ActionLink("Search", "SearchContacts", "SearchString");
    </p>
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

要完全按照您的要求进行操作,您需要一些javascript,因为搜索框中的值是由用户输入的。

但是由于你有一个表单(@using (Html.BeginForm())),你可以改变ActionLink的提交按钮,如:

@using (Html.BeginForm("SearchContacts","YourController", FormMethod.Get))
{
    <p>
        Search First and/or Last Name: @Html.TextBox("SearchString") <br />
        <input type="submit" value="Search" />
    </p>
}