为什么asp.net MVC5搜索方法返回巨大的url

时间:2015-09-06 07:18:13

标签: c# asp.net-mvc entity-framework

我正在尝试实现一种搜索方法,这是控制器:

public ActionResult Index(string SearchBy, string SearchString)
{
    var customers = db.Customers.Include(c => c.ContractType).Include(c => c.City).Include(c => c.County);
    switch (SearchBy)
    {
        case "CustomerName":
        {
            return View(customers.Where(c => c.CustomerName.Contains(SearchString)).ToList()); 
        }
        case "City":
        {
            return View(customers.Where(c => c.City.CityName.Contains(SearchString)).ToList());
        }
        case "County":
        {
            return View(customers.Where(c => c.County.CountyName.Contains(SearchString)).ToList());
        }
        case "StreetAndNumber":
        {
            return View(customers.Where(c => c.StreetAndNumber.Contains(SearchString)).ToList());
        }
        default:
        {
            return View(customers.ToList());   
        }    
    }
}

这是搜索的查看代码:

@using (Html.BeginForm("Index","Customers",FormMethod.Get))
{
    <p>
        @Html.DropDownList("SearchBy", new List<SelectListItem>
        {
            new SelectListItem {Text="Customer Name", Value="CustomerName", Selected=true },
            new SelectListItem {Text="City", Value="City" },
            new SelectListItem {Text="County", Value="County"},
            new SelectListItem {Text="StreetAndNumber", Value="StreetAndNumber"}
        })
        @Html.TextBox("SearchString")
        <input type="submit" value="Search" />
    </p>
}

当我运行此视图并尝试搜索某些内容时,我会收到以下网址:

请求的网址:http://localhost:50822/Customers?SearchBy=CustomerName&SearchString=el &amp; DXScript = 1_225%2C1_164%2C1_130%2C1_127%2C1_202%2C1_213%2C1_207%2C1_210%2C1_129%2C17_36%2C17_3%2C1_206%2C1_218%2C1_146%2C17_8%2C1_208% 2C1_148%2C1_147%2C17_9%2C1_162%2C1_170%2C1_223%2C1_189%2C1_191%2C1_224%2C1_174%2C17_10%2C1_217%2C1_216%2C1_201%2C17_35%2 ....

我不知道URL的大胆部分到底在哪里,而且非常长!!!

有人告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

DevExpress支持团队为此建议了两个答案,每个都有其缺点。

首先是通过JS从表单中手动删除DXScript隐藏的输入。看起来像这样:

document.querySelector('input[name="DXScript"]').remove();

缺点是devexpress使用DXScript告诉它要加载哪些脚本和CSS,这可能会导致问题。

第二种解决方案是将表格GET更改为表格POST。在上面的代码中,您只需将FormMethod.Get更改为FormMethod.Post。这种方法的缺点是,如果您还有其他确定页面内容的查询参数,则如果希望用户能够复制粘贴或添加书签以返回到该确切页面,它们将不再位于URL中。