Saya ingin menyiapkan halaman Detail Karyawan dengan api web asp.net. Dan saya ingin mengambil data dari mongodb dan memfilternya sesuai id. Tapi nilai 'null' muncul di halaman. Apa yang bisa saya lakukan untuk memperbaiki masalah? Bisakah Anda memberi saya ide? Terima kasih sebelumnya.

Halaman Detail Karyawan saya adalah

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MongoDB.Driver;
using MongoDB.Bson;
using TESTMONGO_C.Models;
using System.Configuration;

namespace TESTMONGO_C.Controllers
{
    public class EmployeeDetailDataController : ApiController
    {
        [HttpGet]
        public object GetEmployeeById(string id)
        {
            string constr = ConfigurationManager.AppSettings["connectionString"];
            var Client = new MongoClient(constr);
            var DB = Client.GetDatabase("test");
            var collection = db.GetCollection<EmployeeDetail>("employee").Find(new BsonDocument("_id",ObjectId.Parse(id))).ToList();
            return Json(collection);
        }
    }
}

Model EmployeeDetail saya adalah

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TESTMONGO_C.Models
{
    public class EmployeeDetail
    {
        [BsonRepresentation(BsonType.ObjectId)]
        public string id { get; set; }
        public string name { get; set; }
        public string department { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string country { get; set; }
    }
}
1
RCeyda 8 Agustus 2019, 18:45

1 menjawab

Jawaban Terbaik

Coba ikuti:

    [HttpGet]
    public object GetEmployeeById(string id)
    {
        string constr = ConfigurationManager.AppSettings["connectionString"];
        var Client = new MongoClient(constr);
        var DB = Client.GetDatabase("test");
        var collection = DB.GetCollection<EmployeeDetail>("employee");
        return Json(collection.Find(new BsonDocument("id", id)).ToList());
    }
0
GSazheniuk 9 Agustus 2019, 13:57