2012-10-31 25 views
16

我有一個表格聲明我Razor視圖添加額外的參數,形成提交

<form method="post" action="/Lot/[email protected]" id="filterForm"> 

(順便說一句,我選擇寫出來這樣的,而不是使用Html.BeginFrom因爲我需要給它一個id和沒「知道怎麼做,與Html.BeginFrom - 但是,這是不是這裏的問題)

外面這種形式我有一個按鈕,提交此表(也有一個提交按鈕的形式)

<input type="button" onclick="$('#filterForm').submit();" value="Show all" /> 

現在,問題是,如果這個按鈕是用來提交表單我想在表單中的行動,改變到

action="/Lot/[email protected]&showAll=true" 

如何改變的行動,並通過這個額外的參數?有沒有更好的方法來完成這一切?

回答

29

追加查詢字符串參數到形式的行動,並試圖在運行時改變是棘手的(但並非不可能)。更容易是使用隱藏域:所以現在

<form method="post" action="/Lot/LotList" id="filterForm"> 
    <input type="hidden" name="auctionEventId" value="@auctionEventId" /> 
    ... 

所有你需要做的就是添加一個隱藏字段爲「SHOWALL」

<form method="post" action="/Lot/LotList" id="filterForm"> 
    <input type="hidden" name="auctionEventId" value="@auctionEventId" /> 
    <input type="hidden" name="showAll" value="false" id="showAllField" /> 
    ... 

而就勾上你SHOWALL按鈕的jQuery事件:

<input id="showAllButton" type="button"/> 

的jQuery:

$('#showAllButton').click(function(){ 
    $('#showAllField').val("true"); 
    $('#filterForm').submit(); 
}); 
+0

謝謝你的幫助傑米。 –

4

怎麼樣把一個隱藏的輸入

<form method="post" action="/Lot/[email protected]" id="filterForm"> 

<input type="hidden" id="showAll" name="showAll" value=""/> 

在您的按鈕

<input type="button" onclick="submitForm()" value="Show all" /> 

在腳本的某處

function submitForm(){ 
$('#showAll').val('true'); 
$('#filterForm').submit(); 

} 
4

我知道你說這是不是問題,但你可以這樣來Html.BeginForm添加屬性:

@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new { id = "filterform" })){ 
2

如果你是一個表單中您可以將路由添加到一個按鈕元素。您可以使用formAction和formMethod覆蓋表單的本地操作。您也可以與其他formActions多個按鈕和formMethods

<form action="/somewhere" method="get"> 
 
    <input type="type" name="name" value=" " /> 
 

 

 
    <button formaction="/Lot/[email protected]&showAll=true" formmethod="post">Show All</button> 
 
</form>

0

你好,我發現這工作得很好

@using (Html.BeginForm("ActionName","ControllerName", new { id = "filterform" },FormMethod.Post)){ 

我已經厭倦了做「FormMethod.Post」在param之前,它沒有工作,也沒有通過價值。只有通過試驗和錯誤才能解決問題,並發現它應該像我發佈的代碼。

希望可以幫到

相關問題