[英]ASP.NET MVC 5 - ajax.beginform() with null parameters
I'm working on web application project and I'm trying to include search using ajax. 我正在研究Web应用程序项目,并且正在尝试包括使用Ajax进行的搜索。
I created a search form using ajax.beginform() and I have a little problem: When my textbox field is empty and I click on search I want the view to return all the entities(like no search is occurred) , but it returns empty view. 我使用ajax.beginform()创建了一个搜索表单,但有一个小问题:当我的文本框字段为空并且单击搜索时,我希望视图返回所有实体(就像未进行搜索一样),但是它返回空视图。 I tried to check in the controller if the string is null but no success.
我试图在控制器中检查字符串是否为null但没有成功。
1.what value the parameter gets when the text field is empty? 1.当文本字段为空时,参数获得什么值?
2.how do I send couple of parameters in this form? 2.如何以这种形式发送几个参数?
Thank you in advance! 先感谢您!
Aviv 阿维夫
.cshtml - View .cshtml-查看
@using (Ajax.BeginForm("BranchSearch", "Branches",
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResults" }))
{
<h3>Search:</h3>
<p>Branch name :</p>@Html.TextBox("Search", null, new { id = branchname"})
<input type="submit" value="Search" class="btn btn-primary" />
}
.cs - Controller .cs-控制器
public PartialViewResult BranchSearch(String branchname, String country)
{
List<Branches> model = (from p in db.Branches
select p).ToList();
if(branchname!=null)
{
model = model.Where(x => x.BranchName.Equals(branchname)).ToList();
}
return PartialView("BranchSearch",model);
}
When user does not enter anything in the input search box and submit the form, the script will send an empty string. 当用户未在输入搜索框中输入任何内容并提交表单时,脚本将发送一个空字符串。 So you should check for null or empty string.
因此,您应该检查null或空字符串。
if (!string.IsNullOrEmpty(branchname))
{
model = model.Where(x => x.Branchname.Equals(branchname)).ToList();
}
Also your action method parameter name should match with your input element name. 此外,您的操作方法参数名称应与您的输入元素名称匹配。
@Html.TextBox("branchname")
Also, You do not need to call ToList()
before your Where
clause. 另外,您无需在
Where
子句之前调用ToList()
。 You can call that at the very end and that time the LINQ query expression will be evaluated and will give you the filtered results. 您可以在最后调用该函数,然后将评估LINQ查询表达式并为您提供过滤后的结果。 If you want to use the case insensitive search, use one of the case insensitive
StringComparison
enum value in the Equals
method overload. 如果要使用不区分大小写的搜索,请在
Equals
方法重载中使用不区分大小写的StringComparison
枚举值之一。
public PartialViewResult BranchSearch(String branchname, String country)
{
IQueryable<Branch> model = db.Branches;
if (!string.IsNullOrEmpty(branchname))
{
model = model.Where(x => x.BranchName.Equals(branchname
,StringComparison.OrdinalIgnoreCase));
}
// Now we are ready to query the db and get results. Call ToList()
var result = model.ToList();
return PartialView("BranchSearch", result);
}
If you want to execute multiple filters , add another Where clause on the model
before you call ToList()
(Same as what we did for branchName) 如果要执行多个过滤器,请在调用
ToList()
之前在model
上添加另一个Where子句(与对branchName所做的操作相同)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.