如何使用asp.net mvc在mongodb中保存新文档?

时间:2016-05-06 14:31:44

标签: asp.net asp.net-mvc mongodb

我真的想尝试这样做,因为我开始使用mongo db,但无法找到答案以及如何实现这一目标,我认为我接近于了解它,但需要一点帮助;所以我有一个这样的模型:

public class UserInformationViewModel
{
    [DisplayName("Nom")]
    public string NomUser { get; set; }

    [DisplayName("Prénom")]
    public string PrenomUser { get; set; }

    [DisplayName("Entreprise")]
    public string EntrepriseUser { get; set; }

    [DisplayName("Email")]
    public string EmailUser { get; set; }

    [DisplayName("Téléphone")]
    public string TelephoneUser { get; set; }
}

我在模态中显示如下:

<div id ="test" class="form-panel-modify">
    <div class="form-horizontal style-form">
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.NomUser, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.NomUser, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.PrenomUser, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.PrenomUser, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.EntrepriseUser, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.EntrepriseUser, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.EmailUser, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.EmailUser, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.TelephoneUser, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.TelephoneUser, new { @class = "form-control" })
            </div>
        </div>
    </div>
</div>

模态:

            <div class="modal-content" style="height:600px;width:600px;text-align:center">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Informations</h4>
                    <p>Fill the form</p>
                </div>
                @Html.Partial("_ModalContent")
            </div>

所以我想在表单中获取不同的信息,并将它们保存为我的mongo数据库中的文档,但我无法弄清楚如何做到这一点......我在网上找到的一切似乎都是过时或不回答我的问题。而且,通过阅读mongo网站上的文档,我无法实现这一目标,所以如果有人可以提供帮助。

会很棒!非常感谢,希望我能清楚。

编辑:

public class ConnectionHandler<T> where T : IMongoEntity
{
    public MongoCollection<T> MongoCollection { get; private set; }

    public ConnectionHandler()
    {
        var connectionString = "mongodb://IPADRESS";

        //// Acceder à la chaine de connection
        var mongoClient = new MongoClient(connectionString);

        //// Acceder au serveur
        var mongoServer = mongoClient.GetServer();

        //// Acceder à la BD 
        const string databaseName = "BD NAME";
        var db = mongoServer.GetDatabase(databaseName);
        //// Acceder aux collection la BD (tables en langage relationnel...)
        //// NB: le nom des collections est mis en minuscule et pluralisé
        MongoCollection = db.GetCollection<T>(typeof(T).Name);
    }
}

1 个答案:

答案 0 :(得分:0)

文档将是您的模型。所以你需要使用mongodb驱动程序来为你的模型访问集合:

var usersCollection = db.GetCollection<UserInformationViewModel>("UserInformationCollection");

然后你就可以在集合上使用MongoDB操作,比如Insert:

public async Task Create(UserInformationViewModel user, CancellationToken token)
{
    await usersCollection.InsertOneAsync(user, cancellationToken: token).ConfigureAwait(false);
}