locked
How To get json result with mathing in where RRS feed

  • Question

  • User-2035378889 posted

    am new in mvc I need to get records with matching triacategore party

    is is my model 

    using PharmaApp.Core.DataModel.Base;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace PharmaApp.Core.DataModel
    {
        [Table("Supplier")]
        public class SupplierModel : BaseModel
        {
            [Required]
            public string  Name { get; set; }        
            public string Email { get; set; }
            public string TrialCategory { get; set; }
            public string AccountType { get; set; }
            public string AccountSubType { get; set; }
            public string PartyType { get; set; }
            public string ItemType { get; set; }
            public string Drcr { get; set; }
            public int Bf { get; set; }
            public DateTime BFDate { get; set; }       
            public string Phone { get; set; }
            public string Address { get; set; }
            public string Notes { get; set; }

        }
    }

    this is my controller

     public JsonResult GetSupplier()
            {
                
                return Json(_supplierRepository.All().Where(x => x.TrialCategory == "party"), JsonRequestBehavior.AllowGet);
            }

    Wednesday, January 6, 2021 1:51 AM

Answers

  • User1312693872 posted

    Hi,ABDULLAH ANOTECH

    I need to get records with matching triacategore party

    Could you please explain more about your needs? I tested your shared code and runs well, your problem lies on how to use Repository? Check

    my demo and feel free to ask if you have more questions:

    Controller:

     public class HomeController : Controller
        {
            private ISupplierRepository _supplierRepository;
            public HomeController()
            {
                _supplierRepository = new SupplierRepository(new MyDbContext());
            }
            public ActionResult Index()
            { 
                return View();
            }
            public JsonResult GetSupplier()
            {
                return Json(_supplierRepository.All().Where(x => x.TrialCategory == "party"), JsonRequestBehavior.AllowGet);
            }
    }

    Repository:

    public class SupplierRepository : ISupplierRepository
        {
            private readonly MyDbContext _db;
    
            public SupplierRepository(MyDbContext applicationDbContext)
            {
                _db = applicationDbContext;
            }
            public IEnumerable<SupplierModel> All()
            {
                return _db.SupplierModels.ToList();
            }
        }
    public interface ISupplierRepository
        {
            IEnumerable<SupplierModel> All();
        }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 6, 2021 7:29 AM

All replies

  • User-1545767719 posted

    What is "_supplierRepository.All()" in your code? What can be obtained from it? Without such information probably nobody knows how the Where method works and what will be the result.

    Wednesday, January 6, 2021 2:44 AM
  • User1312693872 posted

    Hi,ABDULLAH ANOTECH

    I need to get records with matching triacategore party

    Could you please explain more about your needs? I tested your shared code and runs well, your problem lies on how to use Repository? Check

    my demo and feel free to ask if you have more questions:

    Controller:

     public class HomeController : Controller
        {
            private ISupplierRepository _supplierRepository;
            public HomeController()
            {
                _supplierRepository = new SupplierRepository(new MyDbContext());
            }
            public ActionResult Index()
            { 
                return View();
            }
            public JsonResult GetSupplier()
            {
                return Json(_supplierRepository.All().Where(x => x.TrialCategory == "party"), JsonRequestBehavior.AllowGet);
            }
    }

    Repository:

    public class SupplierRepository : ISupplierRepository
        {
            private readonly MyDbContext _db;
    
            public SupplierRepository(MyDbContext applicationDbContext)
            {
                _db = applicationDbContext;
            }
            public IEnumerable<SupplierModel> All()
            {
                return _db.SupplierModels.ToList();
            }
        }
    public interface ISupplierRepository
        {
            IEnumerable<SupplierModel> All();
        }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 6, 2021 7:29 AM
  • User-2035378889 posted

    Thanks dear My Code was fine wheen I cleen and rebuild It works fine 

    thans once again

    Tuesday, January 12, 2021 2:36 PM