C# 如何在MVC的帮助下使用if-else条件传递单个返回值?

C# 如何在MVC的帮助下使用if-else条件传递单个返回值?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我已经创建了两个过滤器搜索框,并在if-else语句的帮助下将值传递给控制器。在if语句中,我创建了两个单独的条件来将返回值传递给视图页面。对于两种不同的条件,我只需要一个返回值 public ActionResult ViewCompany(string Company, string City) { //View All Records var data = dp.Company.SqlQuery("Select * from

我已经创建了两个过滤器搜索框,并在if-else语句的帮助下将值传递给控制器。在if语句中,我创建了两个单独的条件来将返回值传递给视图页面。对于两种不同的条件,我只需要一个返回值

public ActionResult ViewCompany(string Company, string City)
        {
            //View All Records
            var data = dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID  DESC").ToList();
            //return View(dp.Company.Where(x => x.CompanyName.Contains(searching) || searching == null).ToList());

            //Search Function
            var customers1 = from s in dp.Company select s;
            if (Company != null || City != null)
            {
                if (!String.IsNullOrEmpty(Company))
                {
                    customers1 = customers1.Where(s => s.CompanyName.Contains(Company));
                    return View(customers1.ToList());
                }

                if (!String.IsNullOrEmpty(City))
                {
                    customers1 = customers1.Where(s => s.City.Contains(City));
                    return View(customers1.ToList());
                }
            }

            return View(data);
        }
csHTML页面

@Html.BeginForm("ViewCompany", "Company", FormMethod.Get)
{
<div class="modal fade" id="upload3Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="modal-close" data-dismiss="modal" aria-label="Close">
                    <i class="font-icon-close-2"></i>
                </button>
                <h4 class="modal-title" id="myModalLabel">Search Company</h4>
            </div>
            <div class="modal-upload menu-big-icons">
                <div class="modal-upload-cont">
                    <div class="modal-upload-cont-in" style="border-left: none;">
                        <div class="tab-content">
                            <div role="tabpanel" class="tab-pane active" id="tab-upload-3-1">
                                <br />
                                <br />
                                Company Name: @Html.TextBox("Company")
                                <br />
                                <br />
                                City: @Html.TextBox("City")
                                <br />
                                <br />
                                <input type="submit" name="submit" value="Search" class=" btn btn-rounded btn-inline btn-success" />
                            </div><!--.tab-pane-->
                        </div><!--.tab-content-->
                    </div><!--.modal-upload-cont-in-->
                </div><!--.modal-upload-cont-->
            </div>
        </div>
    </div>
</div><!--.modal-->
}
@Html.BeginForm(“ViewCompany”、“Company”、FormMethod.Get)
{
搜索公司


公司名称:@Html.TextBox(“公司”)

城市:@Html.TextBox(“城市”)

}
您可以执行以下操作:

var companies = from s in dp.Company select s;

if(!String.IsNullOrEmpty(Company)) companies = companies.Where(s => s.CompanyName.Contains(Company));

if(!String.IsNullOrEmpty(City)) companies = companies.Where(s => s.City.Contains(City));

return View(companies);
你可以做:

var companies = from s in dp.Company select s;

if(!String.IsNullOrEmpty(Company)) companies = companies.Where(s => s.CompanyName.Contains(Company));

if(!String.IsNullOrEmpty(City)) companies = companies.Where(s => s.City.Contains(City));

return View(companies);

我认为你根本不需要第一个if语句

public ActionResult ViewCompany(string Company, string City)
    {
        // Don't do redurant work

        //Search Function
        var customers = from s in dp.Company select s;
            if (!String.IsNullOrEmpty(Company))
            {
                customers = customers.Where(s => s.CompanyName.Contains(Company));
                return View(customers);
            }

            if (!String.IsNullOrEmpty(City))
            {
                customers = customers.Where(s => s.City.Contains(City));
                return View(customers);
            }

        return View(dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID  DESC"));
    }

并将视图模型签名更改为
IEnumerable
,而不是
List
。使用接口而不是它们的实现进行编码是一种更好的方法。

我认为您根本不需要第一条if语句

public ActionResult ViewCompany(string Company, string City)
    {
        // Don't do redurant work

        //Search Function
        var customers = from s in dp.Company select s;
            if (!String.IsNullOrEmpty(Company))
            {
                customers = customers.Where(s => s.CompanyName.Contains(Company));
                return View(customers);
            }

            if (!String.IsNullOrEmpty(City))
            {
                customers = customers.Where(s => s.City.Contains(City));
                return View(customers);
            }

        return View(dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID  DESC"));
    }

并将视图模型签名更改为
IEnumerable
,而不是
List
。使用接口而不是它们的实现进行编码是一种更好的方法。

类似的东西怎么样

    public ActionResult ViewCompany(string Company, string City)
    {
        //View All Records
        var data = dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID  DESC").ToList();
        //return View(dp.Company.Where(x => x.CompanyName.Contains(searching) || searching == null)).ToList());

        //Search Function
        var customers1 = from s in dp.Company select s;
        if (Company != null || City != null)
        {
            return !String.IsNullOrEmpty(Company) ?
                View(customers1.Where(s => s.CompanyName.Contains(Company)).ToList())
                : View(customers1.Where(s => s.City.Contains(City)).ToList());
        }

        return View(data);
    } 
编辑:

好吧,我不明白你想要什么,试试这个:

        if (Company != null || City != null)
        {
            if (!string.IsNullOrEmpty(Company))
            {
                customers1 = (!string.IsNullOrEmpty(City)) ?
                    customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
                    : customers1.Where(s => s.CompanyName.Contains(Company));
            }

            if (!string.IsNullOrEmpty(City))
            {
                customers1 = (!string.IsNullOrEmpty(Company)) ?
                    customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
                    : customers1.Where(s => s.City.Contains(City));
            }

            return customers1.ToList();
        }

像那样的怎么样

    public ActionResult ViewCompany(string Company, string City)
    {
        //View All Records
        var data = dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID  DESC").ToList();
        //return View(dp.Company.Where(x => x.CompanyName.Contains(searching) || searching == null)).ToList());

        //Search Function
        var customers1 = from s in dp.Company select s;
        if (Company != null || City != null)
        {
            return !String.IsNullOrEmpty(Company) ?
                View(customers1.Where(s => s.CompanyName.Contains(Company)).ToList())
                : View(customers1.Where(s => s.City.Contains(City)).ToList());
        }

        return View(data);
    } 
编辑:

好吧,我不明白你想要什么,试试这个:

        if (Company != null || City != null)
        {
            if (!string.IsNullOrEmpty(Company))
            {
                customers1 = (!string.IsNullOrEmpty(City)) ?
                    customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
                    : customers1.Where(s => s.CompanyName.Contains(Company));
            }

            if (!string.IsNullOrEmpty(City))
            {
                customers1 = (!string.IsNullOrEmpty(Company)) ?
                    customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
                    : customers1.Where(s => s.City.Contains(City));
            }

            return customers1.ToList();
        }


空记录仅查看您的意思是什么?您如何显示返回的结果?并确保您有通过这些筛选的数据。您是否查看客户对象?空记录仅查看您的意思是什么?您如何显示返回的结果?并确保您有通过这些筛选的数据。您是否查看客户对象?查看我的最后一张图片是的,
.Contains(city)
,后面的括号错误,我正在编辑答案在您的代码中只列出公司名称,但城市字段不显示编辑,因为您已经在if条件下测试了这一点,所以如果公司为空,则城市不显示。当我分别输入公司名称和城市名称时,它们工作正常。但当我同时输入公司名称和城市名称时,仅显示公司名称值已传递,但城市值不起作用请参见我的上一张图像是的,
.Contains(city)
,后面的括号错误,我正在编辑答案在您的代码中只列出公司名称,但城市字段不显示编辑,因为您已经在if条件下测试了这一点,所以如果公司为空,则城市不显示。当我分别输入公司名称和城市名称时,它们工作正常。但当我同时输入公司名称和城市名称时,仅显示公司名称值已通过,但城市值不起作用