简体   繁体   English

ASP.NET MVC 5-具有空参数的ajax.beginform()

[英]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.

相关问题 asp.net mvc Ajax.BeginForm复制元素 - asp.net mvc Ajax.BeginForm duplicating elements 带有ASP.NET MVC 4的Ajax.BeginForm没有调用控制器动作 - Ajax.BeginForm with ASP.NET MVC 4 not calling controller action Ajax.BeginForm 向控制器操作 ASP.NET MVC 发送空值 - Ajax.BeginForm sends null values to controller action ASP.NET MVC Asp.Net MVC和部分视图以及混合Ajax.BeginForm和Html.BeginForm - Asp.Net MVC and Partial Views and Mixing Ajax.BeginForm and Html.BeginForm Asp.net MVC 4 Ajax.BeginForm提交+ mvc.grid-页面重新加载 - Asp.net MVC 4 Ajax.BeginForm submit + mvc.grid - page reloading ELMAH不记录从ASP.NET MVC Ajax.BeginForm引发的异常 - ELMAH doesn't log exceptions raised from ASP.NET MVC Ajax.BeginForm ASP.NET MVC:如何在页面加载后更改Ajax.BeginForm将发布到的位置? - ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads? ASP.Net MVC Ajax.BeginForm文件上传响应问题 - ASP.Net MVC Ajax.BeginForm File Upload Response Issue 需要使用asp.net MVC3 HTML5剃须刀的ajax.beginform中的uploadcontrol - Need uploadcontrol in a ajax.beginform using asp.net mvc3 html5 razor asp.net mvc 项目在使用 Ajax.BeginForm 时不显示带有自定义注释的错误消息 - asp.net mvc project does not display error message with custom annotation when using Ajax.BeginForm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM