Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC:将搜索属性放入url(而不是查询字符串)_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC:将搜索属性放入url(而不是查询字符串)

C# ASP.NET MVC:将搜索属性放入url(而不是查询字符串),c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,在我的ASP.NET MVC 5应用程序上实现了过滤。 我的搜索框由几个带有预定义值的下拉列表组成。当Dropdownlist值更改时,表单将被提交,我将看到特定方法的所有可用票证 表单提交后,页面将重新加载,我看到的url类似于mysite.com/?Method=car。但是我想去掉查询字符串,将car直接放到url中,即。 mysite.com/method/car或mysite.com/method/plain等 可能吗 搜索框 @using (Html.BeginForm("Searc

在我的ASP.NET MVC 5应用程序上实现了过滤。 我的搜索框由几个带有预定义值的下拉列表组成。当Dropdownlist值更改时,表单将被提交,我将看到特定方法的所有可用票证

表单提交后,页面将重新加载,我看到的url类似于
mysite.com/?Method=car
。但是我想去掉查询字符串,将car直接放到url中,即。
mysite.com/method/car
mysite.com/method/plain

可能吗

搜索框

@using (Html.BeginForm("Search", "Transfer", FormMethod.Get))
{
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Method, Model.Methods, new { @class = "query"})
            </div>
        </div>
    </div>
    <input type="submit" class="hidden" />
}
@使用(Html.BeginForm(“搜索”、“传输”、FormMethod.Get))
{
@DropDownListFor(model=>model.Method,model.Methods,new{@class=“query”})
}
我的行动方法

[Route("~/transfer/{method?}")]
public async Task<ActionResult> List(string method)
{
    //filter and displaying
    return View(viewModel);
}
[路由(“~/transfer/{method?}”)]
公共异步任务列表(字符串方法)
{
//过滤与显示
返回视图(viewModel);
}

默认情况下
Html.BeginForm
FormMethod.Get
将所有表单值序列化为查询字符串。因此,如果您想要友好的URL,您必须编写一些JavaScript(本例中为jQuery)

首先,您可以删除表单

<div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Method, Model.Methods, new { @class = "query"})
            </div>
        </div>
    </div>
<button type="button" id="submitMethodBtn">Submit</button>

<script>
   var baseUrl = '@Url.Action("Search", "Transfer")/'
   $('#submitMethodBtn').click(function(){
       var url = baseUrl + $(#'@Html.IdFor(m=>m.Method)').val();
       window.location.href = url;
   })
</script>

@DropDownListFor(model=>model.Method,model.Methods,new{@class=“query”})
提交
var baseUrl='@Url.Action(“搜索”、“传输”)/'
$('#submitMethodBtn')。单击(函数(){
var url=baseUrl+$(#'@Html.IdFor(m=>m.Method)).val();
window.location.href=url;
})
一些问题/澄清:

1如果您在浏览器中输入
mysite.com/method/car
(假设问题#2已解决),它将引导您采取正确的操作,因此您的问题基本上是在视图中生成友好的Url

2在提供的代码示例中,我使用了一个内联脚本。您可以将其解压缩到一个单独的静态文件中,但您必须对方法属性的url和html id进行硬编码


3您的操作方法将int作为参数,那么您希望它如何与
mysite.com/method/car
(car是一个字符串)?

您可以在页面上调用ajax方法,该方法将避免查询字符串,当控制器重定向到ActionResult时,您可以再次在重定向页面上调用ajax方法。这样可以避免MVC中的查询字符串。 以下面的代码为例

$.ajax({  
      type: "POST",  
      contentType: "application/json; charset=utf-8",  
      url: "Transfer/search",  
      data: "{ID:1}",  
      dataType: "json",  
      error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); }
 });

将返回类型设为控制器方法的JsonResult。

我通常在路由配置文件中设置路由。将{method?}设置为属性时是否需要在其中添加“?”?是的,我需要。“方法”在路由设置中未声明为可选。所以我应该在这里声明谢谢!3号。对,我现在在项目中使用int-tight,但对于这个示例,使用了slug,所以忽略了这些更改。。在样本中修正了,是的。但这种方法不利于搜索引擎优化。这就是为什么我需要用户友好的url