2013-07-20 22 views
0

網址,我有以下formin我的觀點:更好的結構,從MVC beginform

@using (Html.BeginForm("Index", "ToolsForHire", FormMethod.Get)) 
{ 

    <p>tool required: @Html.TextBox("tool")</p> 
    <input type="submit" value="search" /> 
} 

提交這份表格員額:

localhost:54124/ToolsForHire?tool=mytooltext 

這一切工作正常。

但是我想發生的是,我的網址結束是:

localhost:54124/ToolsForHire/mytooltext 

仍然得到與MVC交手所以有些建議是好的?

回答

0

您需要更改URL中global.ascx.cx文件重寫

+0

請問您可以擴展一個例子嗎? – Simon

+0

啊!您可以將Httpget改爲Httppost @using(Html.BeginForm(「Index」,「ToolsForHire」,FormMethod.Post))並在控制器中添加一個帶名稱的參數(字符串工具) – Miller

+0

是的,但不會將它包含在url將它,看到我上面的最新評論... – Simon

1

您可能需要使用以下BeginForm擴展方法BeginForm(action, controller, routevalues, FormMethod)

凡routevalues是沿

東西線
new {tool = mytooltext} 

一般情況下,默認路由設置爲期望一個ID可選參數,因此路由值爲

@using (Html.BeginForm("Index", "ToolsForHire", new {id = mytooltext}, FormMethod.Get)) 

然後url的結果是http:// localhost/ToolsForHire/[mytooltext的值]

希望這有助於。

+0

我得到你說的mytooltext儘管是進入「工具」文本框中的文本,我怎麼能到新的{id = mytooltext}? – Simon

+0

只是爲了澄清,「工具」文本框是在客戶端的文本框,對嗎?所以你想要發生什麼是爲他們提供一個表格,當他們填充「工具」文本框,價值將通過提交?如果是這種情況,你將要使用JavaScript來更新表單操作屬性。那是你想要做的嗎? – ermagana

+0

不好意思回覆。是的,「工具」是一個文本框。所以我希望他們輸入它,提交,然後將該值作爲一個整潔的URL傳遞,所以它將從url/toololshire/somevaluefromtooltextbox – Simon