形成与MVC路线的价值得到 [英] form get with value in mvc route

查看:83
本文介绍了形成与MVC路线的价值得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与mvc4工作,并有一个字段,用户可以在向四周传递给url并重定向到另一页例如输入字段中输入一些文字。 /搜索/<>

I am working with mvc4 and have a field where a user can enter some text in to an input field which gets passed to the url and redirect to another page eg. /search/<>

我的形式如下,但作为重定向查询字符串。

My form is as follows but redirects as query string.

<form class="search" action="@Url.Action("Index", "Search")">
<div>
    <input name="q" value="" type="search" />
    <input type="image" name="btn" value="search" src="@Url.Content("/image1.jpg")" />
</div>

</form>

任何想法,我怎么可能会改变我的形式已输入值,输入Q传递给url。

Any idea how I could alter my form to pass the inputed value to input "q" to the url.

推荐答案

您可以使用 GET 方法:

<form class="search" action="@Url.Action("Index", "Search")" method="get">
    <div>
        <input name="q" value="" type="search" />
        <input type="image" name="btn" value="search" src="@Url.Content("~/image1.jpg")" />
    </div>
</form>

您也可以使用,其目的是实现这一目的的 Html.BeginForm 辅助生成此相同的标记:

You could also generate this exact same markup using the Html.BeginForm helper which is designed for this purpose:

@using (Html.BeginForm("Index", "Search", FormMethod.Get, new { @class = "search" }))
{
    <div>
        <input name="q" value="" type="search" />
        <input type="image" name="btn" value="search" src="@Url.Content("~/image1.jpg")" />
    </div>
}

当您使用GET方法,所有的输入元素值将在当表单提交的查询字符串被发送。

When you use the GET method all input element values will be sent in the query string when the form is submitted.

如果你要追加在url中的路径部分搜索字符串,而不是使用查询字符串参数的我请你阅读<一href=\"http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx\"相对=nofollow> 下面的博客文章 斯科特Hanselman的。我将引用只是他的结论是:

And if you want to append the search string in the path portion of the url instead of using a query string parameter I invite you to read the following blog post from Scott Hanselman. I will quote only his conclusion:

这一切的努力,让疯狂的事情在请求路径后,它的
  值得一提的只是保持值作为查询的一部分
  字符串(记得回来的路上在这篇文章的开头?)比较容易,
  更清洁,更灵活,更安全。

After ALL this effort to get crazy stuff in the Request Path, it's worth mentioning that simply keeping the values as a part of the Query String (remember WAY back at the beginning of this post?) is easier, cleaner, more flexible, and more secure.

这篇关于形成与MVC路线的价值得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆