html 在www.example.com核心mvc中使用多个字段进行搜索asp.net

8zzbczxx  于 2022-12-02  发布在  .NET
关注(0)|答案(1)|浏览(144)

我在www.example.com核心mvc中使用sql查询创建从数据库中获取数据的表单asp.net。现在我想使用多个字段搜索数据,如价格、面积、面积单位。我是c#新手,请帮助我搜索数据并显示。数据显示在其他div中。在该div中,我使用foreachloop制作卡片并从数据库中获取数据。我的视图:

@model IEnumerable<eHouse.Models.RentModel>

 <form>
                    <div class="wrap">
                        <div class="search">
                            <input type="text" class="searchTerm" style="width: 700px; color:#000000; text-align: left;" placeholder="Search Houses" onclick="filterfunction()">
                            <button type="submit" class="searchButton" >
                                <i class="fa fa-search"></i>

                            </button>
                        </div>
                        <div id="filter">
                        <select>
                            <option value="Kanal">Kanal</option>
                            <option value="Marla">Marla</option>
                            <option value="Square Feet">Square Feet</option>
                            <option value="Square Meter">Square Meter</option>
                            <option value="Square Yards">Square Yards</option>
                        </select>
                        <input type="text" placeholder="area" />
                        <input type="text" placeholder="price" />

                    </div>
                    </div>
                    
                </form>
<div class="property_information" >

           @foreach (var item in Model)
           {
                <div class="home-info">
                    <span id="houseid">
                          
                    </span>
                  
                    <a href="#" class="home-images">
                        <img src="@item.pic1" />
                    </a>
                    <div class="home-data">
                        <div class="home-name">
                            <p>@item.tittle</p>
                        </div>
                       
                        <div class="price">
                            <p>@item.price</p>
                        </div>
                        
                        <div class="features">
                            <span>
                               @item.bedroom
                            </span>
                            <span>
                                 @item.bathroom
                            </span>
                            <span>
                                2
                            </span>
                        </div>
                       
                        <div class="desc">
                            @item.descrip
                        </div>
                        <div class="contact-save">
                            <a href="#" class="phone_number" id="favorite" onclick="Fav(this)" data-id="@item.id" >
                                <i class="fas fa-heart" style=" color: white;"></i>
                            </a>
                        
                            <div class="popup" onclick="myFunction()">
                            <a href="tel:+928754756478" class="phone_number" onclick="call()">
                            </a>
                            </div>
                            <div class="popupmsg" onclick="myFunctionmsg()">
                               <a href="#" class="phone_number open_message" onclick="msg()">
                               </a>
                            </div>
                            <a href="#" class="phone_number" onclick="del(this)" data-id="@item.id">
                                <i class="fas fa-trash-alt"  style=" color: white;"></i>
                            </a>
                        </div>
                    </div>
                </div>
            }
    </div>

我的控制器:

public IActionResult Rent(int PageNumber = 1)
{
    var data = rdb.GetDataHouse();
    var datas = rdb.GetDataHouse();
          
    ViewBag.Data = datas.ToList().Take(6);
          
    ViewBag.Totalpages = Math.Ceiling(data.Count()/6.0);

    data = data.Skip((PageNumber - 1) * 6).Take(6).ToList();

    return View(data);
}

q3aa0525

q3aa05251#

通过表单中的输入提交传递过滤器数据。您将需要新的viewModel和您的data、用于post的input以及filterList。您需要类似于以下内容的内容:

@model YourViewModel;

//Your form

<label asp-for="Input.Name"></label>
<select asp-for="Input.Name" asp-items="@(new SelectList(Model.SearchList))" class="form-control">
<option></option>
</select>
<span asp-validation-for="Input.Name" class="text-danger"></span>

<input type="submit" value="Search" class="btn btn-primary"/>

//your loop
@foreach (var item in Model.Data)

//Contoller

public IActionResult Rent(int PageNumber = 1, InputModel input)
{
    var data = rdb.GetDataHouse();
    var datas = rdb.GetDataHouse();
          
    ViewBag.Data = datas.ToList().Take(6);
          
    ViewBag.Totalpages = Math.Ceiling(data.Count()/6.0);

    if(!string.InNullOrEmpty(input.Name))
    {
        data = data.Where(x => x.Name == input.Name).ToList();
    }
    
    data = data.Skip((PageNumber - 1) * 6).Take(6).ToList();
    
    var viewModel = new YourViewModel
    {
       Data = data // data,
       SearchList = your List<string>(), //for dropdown
       Input = new YourInputModel(),
    }

    return View(viewModel);
}

//Input model
public class YourInputModel
{
    public string Name { get; set; }
}

//View model
public class YourViewModel
{
    public  YourDataType Data{ get; set; }
    public  List<strings> SearchList { get; set; }
    public  YourInputModel Input { get; set; }
}

你还需要一些其他的东西,希望你能处理好

相关问题