2012-05-05 28 views
1

我想將T4MVC應用到我的項目。說,我有一個Ajax搜索框,它調用Home/SearchQuery操作,它接受一個字符串q作爲參數。我如何在T4MVC中編寫該行?T4MVC和Ajax方法與參數

從Ajax.BeginForm( 「SEARCHQUERY」, 「家」,.... 要Ajax.BeginForm(MVC.Home.SearchQuery(???)...

.cshtml文件

@using (Ajax.BeginForm("SearchQuery", "Home", /* <-----Convert to T4MVC Here */ 
     new AjaxOptions { 
      LoadingElementId = "loadingGif", 
      OnSuccess = "parseResults", 
      OnFailure = "searchFailed" 
     })) { 
    <input type="text" name="q" /> 
    <input type="submit" value="Search" /> 
    <img id="loadingGif" style="display:none" src="@Url.Content("~/content/images/loading.gif")" /> 
} 

<div id="searchResults" style="display: table"></div> 

回答

2

q是從表單的輸入提交的,所以你可以只寫

@using (Ajax.BeginForm(MVC.Home.SearchQuery(), 
     new AjaxOptions { 
      LoadingElementId = "loadingGif", 
      OnSuccess = "parseResults", 
      OnFailure = "searchFailed" 
     })) { 
    <input type="text" name="q" /> 
    <input type="submit" value="Search" /> 
    <img id="loadingGif" style="display:none" src="@Url.Content("~/content/images/loading.gif")" /> 
} 
+0

我不明白你的意思。因爲MVC.Home.SearchQuery()會給你一個語法錯誤,SearchQuery簽名有參數(字符串q),對嗎?像這樣... [OutputCache(NoStore = true,VaryByParam =「」,Duration = 0)] [AcceptVerbs(HttpVerbs.Post)] public virtual JsonResult SearchQuery(string q){ if(!Request.IsAjaxRequest() )返回null; .... //更多代碼 – Tom

+0

那個簽名不匹配的語法錯誤(填充到字符串q中)正是我的問題所在。 – Tom

+1

T4MVC總是產生一個沒有參數的重載,所以@archil建議應該工作。你有沒有嘗試過? –

1

另一種可能的答案:再生模板

我知道這有點愚蠢,但我只是因爲忘記用模板重新生成類(帶有參數的新方法在重新生成模板之前可訪問)而到達此處。也許有人會發現這個有用的。