Я хочу запросить коллекцию монго, но не могу получить доступ к значениям столбцов и не знаю, как записать их в модель, как я могу передать результаты запроса в представление через модель?
Код контроллера:
public ActionResult HelloMongo()
{
var client = new MongoClient("xxxxxx");
var database = client.GetDatabase("y");
var collection = database.GetCollection<BsonDocument>("Countries");
var documents = collection.Find(new BsonDocument()).ToList();
foreach (var document in documents)
{
// how to write documents into Model?
}
return View(viewModel);
}
Не знаю, выглядит ли модель нормально
Код модели:
public class CountryModel
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("CountryCode")]
public string CountryCode { get; set; }
[BsonElement("CountryName")]
public string CountryName { get; set; }
public CountryModel(string v2, string v3)
{
CountryCode = v2;
CountryName = v3;
}
}
Я хочу отобразить результаты запроса встол
Вид:
<h2>Mongo Table Content</h2>
<table class="table table-striped">
<thead>
<tr>
<th>
Country Code
</th>
<th>
Country Name
</th>
</tr>
</thead>
<tbody>
@foreach (var document in Model.documents)
{
<tr>
<td>
@Model.CountryCode
</td>
<td>
@Model.CountryName
</td>
</tr>
}
</tbody>
</table>