【问题标题】:want to make filter product for ecommerce website AspNet Core想为电子商务网站 AspNet Core 制作过滤器产品
【发布时间】:2020-07-12 09:41:53
【问题描述】:

帮助,想为电子商务网站 Asp.Net Core 和剃须刀页面制作具有多个参数(类别)的过滤器产品 不使用文本框我可以做到我需要它与复选框或下拉多 所以请帮助我完成大部分网站,我试过但我做不到。 我用 3 个文本框进行了搜索

    `[HttpGet]
    public async Task<ActionResult> Search(string Id, string Id2, string Id3)
    {
        var Result = from Res in _context.ItEntity.Include(i => i.CaRelation)
                     .Include(i => i.FaRelation)
                     .Include(i => i.ITImages)
                     select Res;
        //var Result = _context.ItEntity.AsQueryable();
        if (!string.IsNullOrEmpty(Id))
        {
            Result = Result.Where(x => x.ITName.Contains(Id));
        }
        if (!string.IsNullOrEmpty(Id2))
        {
            Result = Result.Where(x => x.FaRelation.FaName == Id2);
        }
        if (!string.IsNullOrEmpty(Id3))
        {
            Result = Result.Where(x => x.CaRelation.CaName == Id3);
        }
        return PartialView("_Search", await Result.ToListAsync());
    }

`

【问题讨论】:

  • 您需要更加清楚自己想要实现的目标。而且您还需要清楚地描述您为解决您遇到的问题所做的尝试。否则,没人能帮你。
  • 感谢大卫的建议,我做到了,我正在努力获得更多经验并变得更专业。

标签: c# linq asp.net-core model-view-controller razor-pages


【解决方案1】:

这是一个关于如何使用DropdownList 过滤结果并通过partial view 显示的工作演示,如下所示:

型号:

public class ItEntity
{
    public int Id { get; set; }
    public string ITName { get; set; }
    public string Details { get; set; }
    public CaRelation CaRelation { get; set; }
    public FaRelation FaRelation { get; set; }
    public ITImages ITImages { get; set; }
}
public class CaRelation
{
    public int Id { get; set; }
    public string CaName { get; set; }
}
public class FaRelation
{
    public int Id { get; set; }
    public string FaName { get; set; }
}
public class ITImages
{
    public int Id { get; set; }
    public string ImName { get; set; }
}

Index.cshtml:

@model IEnumerable<ItEntity>
<form method="get" asp-action="Search">
    <div class="form-group">
        <label class="control-label">ITName</label>
        <select id="Id" asp-items="ViewBag.IT"></select>
    </div>
    <div class="form-group">
        <label class="control-label">FaName</label>
        <select id="Id2" asp-items="ViewBag.FaName"></select>
    </div>
    <div class="form-group">
        <label class="control-label">CaName</label>
        <select id="Id3" asp-items="ViewBag.CaName"></select>
    </div>
    <div class="form-group">
        <input type="button" id="btnSearch" value="Search" class="btn btn-primary"/>
    </div>
</form>
<div id="data"></div>
@section Scripts
{
    <script>
        $(function () {
            var filters = {
            Id: null,
            Id2: null,
            Id3: null
            };
            GetData(filters);
        });
        $('#btnSearch').on('click', function (e) {
            var filters = {
                Id: $('#Id').val(),
                Id2: $('#Id2').val(),
                Id3: $('#Id3').val()
            };
            GetData(filters);
        });
        function GetData(filters) {
            $.ajax({
                url: '/Tests/Search',
                type: 'Get',
                cache: false,
                async: true,
                dataType: "html",
                data : filters
                })
                .done(function (result) {
                $('#data').html(result);
                }).fail(function (xhr) {
                console.log('error : ' + xhr.status + ' - ' + xhr.statusText + ' - ' + xhr.responseText);
                });

        }
    </script>
}

_Search.cshtml:

注意:请确保部分视图已位于正确的文件夹中,请参阅here

@model IEnumerable<ItEntity>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ITName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FaRelation.FaName)               
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CaRelation.CaName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Details)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ITName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FaRelation.FaName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CaRelation.CaName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Details)
                </td>
            </tr>
        }
    </tbody>
</table>

控制器:

public class TestsController : Controller
{
    private readonly YourDbContext _context;

    public TestsController(YourDbContext context)
    {
        _context = context;
    }
    // GET: Tests
    public async Task<IActionResult> Index()
    {

        ViewBag.IT = new SelectList(_context.ItEntity.Select(i => i.ITName).ToList());
        ViewBag.FaName = new SelectList(_context.FaRelation.Select(i => i.FaName).ToList());
        ViewBag.CaName = new SelectList(_context.CaRelation.Select(i => i.CaName).ToList());
        return View();
    }
    [HttpGet]
    public async Task<ActionResult> Search(string Id, string Id2, string Id3)
    {
        var Result = from Res in _context.ItEntity.Include(i => i.CaRelation)
                     .Include(i => i.FaRelation)
                     .Include(i => i.ITImages)
                     select Res;

        if (!string.IsNullOrEmpty(Id))
        {
            Result = Result.Where(x => x.ITName.Contains(Id));
        }
        if (!string.IsNullOrEmpty(Id2))
        {
            Result = Result.Where(x => x.FaRelation.FaName == Id2);
        }
        if (!string.IsNullOrEmpty(Id3))
        {
            Result = Result.Where(x => x.CaRelation.CaName == Id3);
        }
        return PartialView("_Search", await Result.ToListAsync());
    }
}

结果:

【讨论】:

  • OMG 感谢 rena 的努力和帮助我,我以为我永远不会得到任何帮助,但我试图从类别下拉列表中传递多个参数以根据它进行过滤, 2 或 3 个类别并获得具有该 2 或 3 个类别的产品
  • 你的意思是你想要一个选择列表,你可以多选项目?
  • 我用复选框做到了,我返回了 viewbag 中的类别并传递了 int 列表,然后返回所有条件为 where (listofint.contains(x.categoryId)) || 的项目listofint.count == 0)
  • 然后我用分页列表返回变量并返回视图模型
  • 如果您对注册确认电子邮件和忘记密码发送带有链接的电子邮件有建议,我将让他们无法使用的身份进行一些修改
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多