2016-05-06 43 views
0

我真的想试试这个,因为我开始使用mongo db但是找不到答案,以及如何实现这一点,我认为我已经接近了解它,但需要一点帮助;所以,我有这样一个模型:如何使用asp.net mvc在mongodb中保存新文档?

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

的HTML代码无您显示的内容是相关的,以及您找到的文档是否适用于您的情况取决于您使用的是哪个版本的驱动程序。 – CodeCaster

+0

您可以共享包含对MongoDB驱动程序代码的引用的相关服务器端代码吗? –

+0

我做了一个编辑,这是我如何连接到我的db –

回答

0

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

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

,然后用您可以使用收集的MongoDB的操作,如插入:

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

你能解释一下吗?我把这个放在我的控制器中吗? 好吧,我认为它会进入用户集合? –

+0

我会将访问MongoDB驱动程序的代码添加到专门的数据访问类。 UserRepository之类的东西。将该UserRepository的一个实例提供给您的UserController。 –

+0

那么我的项目中的参考呢?我使用mongo ref byt仍然有消息“名称MongoCollection不存在于上下文中” –

相关问题