如何在同一视图中获取剃刀文本框的值?

Gau*_*han 5 c# asp.net-mvc razor

我在 Razor 视图页面中使用了 `@Html.Textbox("searchString")。我需要这个文本框的值

@Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = "Value of textbox"}).
Run Code Online (Sandbox Code Playgroud)

当然,html 操作链接中的搜索字符串部分现在不起作用,但我需要这个,因为我有根据搜索字符串工作的特定路线。

如何将文本框的值传递给操作链接?

我检查这个这个这个这个这个

我试过的是

<script type = "text/javascript">
var value = document.getElementbyID("searchString").Text;
var link =  @Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = -1});
link.replace(-1,value);
</script>
Run Code Online (Sandbox Code Playgroud)

仍然没有运气。我了解 Razor 在服务器端渲染。

更新

我在视图顶部有以下文本框:

    @using (Html.BeginForm("Index", "trans_junction", FormMethod.Get))
{
<p>
    Request No: @Html.TextBox("searchString")
    <span><input type="submit" value="Search Request" /></span>
</p>
}
Run Code Online (Sandbox Code Playgroud)

此文本框是用户可以在其中搜索项目的搜索框。

有一个操作链接如下:

 @Html.ActionLink("View Items", "Index", new { id = item.transaction_id }) |
Run Code Online (Sandbox Code Playgroud)

和路由配置:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}/{searchString}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, searchString= UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我需要通过 actionlink 将值作为我的路线图传递。正如 StephenMuecke 在回答中所建议的那样,我尝试修改我的@using (Html.BeginForm("Index", "trans_junction", FormMethod.Get))with@using (Html.BeginForm("Index", "trans_junction", new { id = item.transaction_id }, FormMethod.Get)) 但项目在那里无法访问。

当用户点击搜索按钮时,网址是 http://localhost:57286/trans_junction/Index?searchString=20

如果用户点击操作链接网址是 http://localhost:57286/trans_junction/Index/88

但我实际上需要的是 http://localhost:57286/trans_junction/Index/88/20

保留搜索结果并传递id。

我正在添加屏幕截图以便更好地理解。

这是没有搜索的索引视图。

在此处输入图片说明

这是 searchString = 10 的搜索结果。 在此处输入图片说明

这是在单击操作链接后,即查看项目,此处不保留搜索结果。 在此处输入图片说明

小智 4

不要使用 javascript 手动尝试操作链接,而是使用带有method="get". 在你的循环内

@using (Html.BeginForm("Index", "trans_junction", new { id = item.transaction_id }, FormMethod.Get))
{
    @Html.TextBox("SearchString")
    <button type="submit">View Items"</button> // style as a link if desired
}
Run Code Online (Sandbox Code Playgroud)

这将进行 GET 调用

[HttpGet]
public ActionResult Index(int id, string searchString)
Run Code Online (Sandbox Code Playgroud)

假设您有一个特定的路线定义

url: "trans_junction/Index/{id}/{searchstring}",
defaults: new { controller = "trans_junction", action = "Index" }
Run Code Online (Sandbox Code Playgroud)

编辑(基于更新的问题详细信息)

当您返回过滤结果时,您还需要将 的值传回searchString视图,以便可以在方法中使用 in 生成路由值ActionLink(),例如

ViewBag.Filter = searchString;
return View(....);
Run Code Online (Sandbox Code Playgroud)

然后修改链接为

@Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = ViewBag.Filter })
Run Code Online (Sandbox Code Playgroud)

  • 我现在看到你只有一个文本框(它改变了一切)。我将在一个小时左右重写答案来解决这个问题(本质上,当您根据搜索字符串呈现新结果时,您需要将搜索字符串传回视图(例如使用“ViewBag.Filter = searchString;”) )并使用 `@Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = ViewBag.Filter })` 创建链接 (2认同)