如何强制部分视图正确呈现 url

how to force partial view to render proper url

ASP.NET MVC4搜索Razor局部视图定义为

@inherits ViewBase<MvcMusicStore.ViewModels.SearchViewModel>

@using (Html.BeginForm("Browse", "Store" , FormMethod.Get,
    new { @class = "searchform" }))
{

    @Html.TextBoxFor(model => model.Search, new
{
    @class = "searchfield",
    value = "Search..."
})
    <input class="searchbutton" type="submit" value="@("Search...")" />
    <input type="hidden" value="relevance" name="order" />
}

并用于 site.cshtml:

@Html.Partial("Search", new MvcMusicStore.ViewModels.SearchViewModel())

如果页面地址是 http://localhost:52223/Store/Browse/Kuupakkumine,则使用无效操作创建搜索表单 /Store/Browse/Kuupakkumine。 Kuupakkumine 自动添加到 url:

的末尾
<form action="/Store/Browse/Kuupakkumine" class="searchform" method="get">
 <input class="searchfield" id="Search" name="Search" type="text" value="Search...">    
<input class="searchbutton" type="submit" value="Search...">
</form>

如何解决此问题以便正确的 url、/Store/Browse 将显示在浏览器 html 中?

Storecontroller 浏览方法签名是

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public ActionResult Browse(string category, string order, int? page, bool? descending,
 int? itemsPerPage, string brand, string color, string @class, string type, bool? grid,
    string search, bool? instock, CartLayout? cartLayout, string id)

您使用的默认路由 (url: "{controller}/{action}/{id}",) 和您的 Browse() 方法包含匹配的 string id 参数,这意味着 id 的值被添加为路由价值。 BeginForm() 方法使用这些值生成表单 action 属性。此行为很有用,因为如果您(比如说)创建一个表单来编辑模型,并且该模型包含 属性 int ID,则没有必要为 [=18] 包含隐藏输入=] 属性.

要覆盖默认行为,您需要将路由值显式设置为 null

Html.BeginForm("Browse", "Store", new { id = "" }, FormMethod.Get, new { @class = "searchform" }))

这是由路由引起的,可能您有一个匹配 /Store/Browse/Kuupakkumine (/{Controller}/{Action}/{id}) 的默认路由。

尝试创建一个新的没有 id 的死记硬背,在上面放一个特定的名称,然后像这样在你的 Partial 中使用。

routes.MapRoute("Search", "{Controller}/{Action}");

并在您的部分使用它。

Html.BeginForm("Search")