将表单提交到特定网址

时间:2015-07-17 12:54:30

标签: c# html asp.net-mvc forms

好的,我有这样的表格:

  @using (Html.BeginForm("Search", "Home", FormMethod.Get, new { enctype = "multipart/form-data", @class = "navbar-form navbar-left form-inline", @role = "search" }))
            {
                var numberOfVideos = SessionHelper.NumberOfVideos;
                <div class="icon-addon addon-md">
                    <input type="text" placeholder="Search (@numberOfVideos videos)" class="form-control" name="searchString" id="searchString">
                    <label for="searchString" class="glyphicon glyphicon-search" rel="tooltip" title="search"></label>
                </div>
            }
        </div>

提交后,进入/ Home / Search?searchString = {searchStringValue}

我希望它转到/ search / {searchStringValue}(我有一个很好的路由属性)

1 个答案:

答案 0 :(得分:6)

如果您选择使用JavaScript而不是稍微更正确且可维护的后端修复,

form.onsubmit = function(e) {
    e.preventDefault();
    window.location.href = '/search/' + encodeURIComponent(form.elements[controlName].value);
}

使用JavaScript的主要好处是代码有多短。