简体   繁体   English

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

[英]How do get value of razor textbox in the same view?

I am using `@Html.Textbox("searchString") in Razor view page. 我在Razor视图页面中使用`@ Html.Textbox(“searchString”)。 I need this value of textbox in 我需要这个文本框的值

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

Of course the search string part in html action link is not working right now but i need this as i have specific route which works according to search string. 当然,html动作链接中的搜索字符串部分现在不能正常工作,但我需要这个,因为我有根据搜索字符串工作的特定路由。

How do i pass value of textbox to action link? 如何将文本框的值传递给操作链接?

i check this this , this , this and this . 我检查这个这个这个这个这个

What i tried is 我尝试的是

<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>

Still no luck. 仍然没有运气。 I understand Razor renders at server side. 我理解Razor在服务器端渲染。

UPDATE UPDATE

i have following textbox on the top of view: 我在视图顶部有以下文本框:

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

This textbox is search box in which user can search items. 此文本框是搜索框,用户可以在其中搜索项目。

there is an Action link as follows: 有一个Action链接如下:

 @Html.ActionLink("View Items", "Index", new { id = item.transaction_id }) |

and a route config: 和路由配置:

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 }
        );
    }
}

Now i need to pass the values as my route map via actionlink. 现在我需要通过actionlink传递值作为我的路线图。 As suggested in answer by StephenMuecke, i tried to modify my @using (Html.BeginForm("Index", "trans_junction", FormMethod.Get)) with @using (Html.BeginForm("Index", "trans_junction", new { id = item.transaction_id }, FormMethod.Get)) but item is not accessible there. 正如StephenMuecke在回答中所建议的,我试图用@using修改我的@using (Html.BeginForm("Index", "trans_junction", FormMethod.Get)) @using (Html.BeginForm("Index", "trans_junction", new { id = item.transaction_id }, FormMethod.Get))但在那里无法访问项目。

When user click search button, url is http://localhost:57286/trans_junction/Index?searchString=20 当用户点击搜索按钮时,url为http://localhost:57286/trans_junction/Index?searchString=20

if user click on Action link url is http://localhost:57286/trans_junction/Index/88 如果用户点击Action链接url是http://localhost:57286/trans_junction/Index/88

but i actually need is http://localhost:57286/trans_junction/Index/88/20 但实际上我需要的是http://localhost:57286/trans_junction/Index/88/20

which preserve the the result of search and also pass the id. 保留搜索结果并传递id。

I am adding screenshots for better understanding. 我正在添加屏幕截图以便更好地理解。

This is index view without search. 这是没有搜索的索引视图。

在此输入图像描述

This is search result with searchString = 10. 这是searchString = 10的搜索结果。 在此输入图像描述

This is after clicking the action link ie View Items, Here search results are not preserved. 这是在单击操作链接后查看项目,此处不保留搜索结果。 在此输入图像描述

Rather than manually trying to manipulate a link using javascript, use a form with method="get" . 而不是手动尝试使用javascript操纵链接,使用method="get"的表单。 Inside your loop 在你的循环内

@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
}

which will make a GET call to 这将进行GET调用

[HttpGet]
public ActionResult Index(int id, string searchString)

which assumes you have a specific route definition with 假设您具有特定的路径定义

url: "trans_junction/Index/{id}/{searchstring}",
defaults: new { controller = "trans_junction", action = "Index" }

Edit (based on updated question details) 编辑 (根据更新的问题详细信息)

When your returning the filtered results, you will also need to pass back the value of searchString to the view so that in can be used to generate the route value in the ActionLink() methods, for example 当您返回过滤结果时,您还需要将searchString的值传回视图,以便in in可用于在ActionLink()方法中生成路由值,例如

ViewBag.Filter = searchString;
return View(....);

and then modify the links to 然后修改链接

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

You need to set the value on an event such as clicking on the ActionLink otherwise how do you know the value of the searchString textbox to replace in the Actionlink. 您需要在事件上设置值,例如单击ActionLink,否则您如何知道要在Actionlink中替换的searchString文本框的值。 Add this javascript on your page. 在您的网页上添加此JavaScript。

$(function(){
    $("#myLink").click(function(){
        var link = $(this).attr("href");
        link.replace(-1, $(#searchString).val());
        var link = $(this).attr("href", link);
    });
});​​​​

And you Razor view actionlink needs to be 而你Razor查看actionlink需要

@Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = "-1"})

Try: 尝试:

  @{
     var value = Request.Form["inputID"];
    }

ps why aren't you using a form in stead of the link and submit the form when someone enters a searchString ? ps为什么你不使用表单而不是链接并在有人输入searchString时提交表单?

whoever downvoted this answer you could argument why or come up with a better solution? 无论谁低估了这个答案你都可以论证为什么或提出更好的解决方案? because in his case, searchString = Request.Form["inputID"]; 因为在他的情况下, searchString = Request.Form["inputID"]; should do what he is asking for. 应该做他想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM