如何将信息从UI保存到mongoDB集合

时间:2016-05-11 13:16:03

标签: c# asp.net-mvc mongodb razor

我正在寻找这个,我有一个包含一些文本框的模态,用户必须填充它们然后我想将信息保存在mongo的集合中。 ConnectionHandler.cs类用于连接数据库,我们使用的是mongoentities。所以我有这个:

    using MongoDB.Driver;
    using Project.My.Core.Entities.MongoEntities;

   namespace Project.My.Core
{
     public class ConnectionHandler<T> where T : IMongoEntity
     {
        public MongoCollection<T> MongoCollection { get; private set; }

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

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

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

          //// Acceder à la BD 
          const string databaseName = "MyDB";
          var db = mongoServer.GetDatabase(databaseName);
          //// Acceder aux collection la BD (tables en langage relationnel...)
          MongoCollection = db.GetCollection<T>(typeof(T).Name);

    }
}

}

我的模态内容位于部分视图中:

@using Project.My.Web.Administration.Models
@model GarantieViewModel

<div id ="userinput" 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 class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.whoareyou, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.EnumDropDownListFor(m => m.UserInformationViewModel.whoareyou, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.mutuelle45, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.EnumDropDownListFor(m => m.UserInformationViewModel.mutuelle45, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.UserInformationViewModel.NumeroOrias, new { @class = "col-sm-d col-sm-3 control-label" })
            <div class="col-sm-8">
                @Html.TextBoxFor(m => m.UserInformationViewModel.NumeroOrias, new { @class = "form-control" })
            </div>
        </div>
    </div>
</div>
<div style="margin-top:50px;">
<input type="submit" disabled="true" class="btn btn-default" style="color:    #ffffff;background-color:#48bcb4;border-color: #48bcb4;">
</div>

最后我的模态就在这里:

        <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content" style="height:650px;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><b>Pour lancer la comparaison vous devez d'abord renseigner vos informations</b></p>
                </div>
                @Html.Partial("_ModalContent")
            </div>

        </div>
    </div>

所以,在到处寻找之后,我无法做我想做的事。我只是希望我的模态内容的信息进入名为“UserInformation”的集合中。例如。

如果有人可以帮忙解决这个问题,那就太好了!感谢。

0 个答案:

没有答案