2012-06-01 14 views
2

我有一個partialView在我的佈局頁的搜索文本輸入和其他3頁使用的佈局:回到前面的頁

@using (Html.BeginForm("Search", "Product")) 
{ 
    @Html.TextBoxFor(m => m.SearchText) 
    @Html.ValidationMessageFor(m => m.SearchText) 

    <input type="submit" value="Search"/> 
} 

當它進入產品內部/搜索行動,我怎麼能知道在哪裏它來自並返回到正確的頁面,並假設其他3個頁面使用不同的ViewModels?

回答

2

在你的佈局,修改BeginForm包括在路由值請求路徑:

public ActionResult Search(string path, FormCollection form) { 
    // build your search results here 
    return Redirect(path); 
} 

@using (Html.BeginForm("Search", "Product"),new {path = Request.Path}) 

然後,在你的控制器,你可以重定向到該路徑完成了

而這應該做的伎倆。如果你需要傳遞下去的更多信息,你可以查詢字符串只是附加到路徑:

public ActionResult Search(string path, FormCollection form) { 
    // build your search results here 
    return Redirect(path + "?message=foo"); 
} 
+0

不錯..但我需要顯示一個消息,說什麼不順心,我怎麼能帶重定向的消息? – MuriloKunze

+0

我可以在重定向後清除網址嗎?因爲在url中添加了一個查詢字符串。 – MuriloKunze

+0

我編輯了帖子,以顯示如何傳遞消息。不過,我不確定「重定向後清除URL」是什麼意思。 –