CRUD Operation In Lotus Notes Via ASP MVC

In this article, we are going to learn how to perform a CRUD operation in Lotus Notes via ASP MVC.
 
Previously, I have posted an article "How to get data from Lotus Notes through ASP MVC" . To know about this, refer to Getting Data From Lotus Notes Through ASP MVC 
 
(Note: The viewers are asked to read the previous article, because we are continuing the same model and controller here).
 
In the last article, we learned how to get the data from Lotus Notes view and displayed it in ASP MVC list.
 
 
Here, our model is StudentDetails.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace LotusNotesTOASPMvc.Models  
  7. {  
  8.     public class StudentDetails  
  9.     {  
  10.         public string UNID { getset; }  
  11.         public string StudentName { getset; }  
  12.         public double Age { getset; }  
  13.         public string City { getset; }  
  14.         public string Address { getset; }  
  15.         public string PhoneNo { getset; }  
  16.     }  

Our controller is "StudentInformationsController".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Domino;  
  7. using LotusNotesTOASPMvc.Models;  
  8.   
  9. namespace LotusNotesTOASPMvc.Controllers  
  10. {  
  11.     public class StudentInformationsController : Controller  
  12.     {  
  13.         NotesSession session = new NotesSession();  
  14.         NotesDatabase db;  
  15.         NotesView view;  
  16.         NotesDocument doc;  
  17.         public ActionResult Index()  
  18.         {  
  19.             session.Initialize("password@#123");  
  20.             db = session.GetDatabase("""Migration.nsf"false);  
  21.             view = db.GetView("vwStudentInformations");  
  22.             doc = view.GetFirstDocument();  
  23.             List<StudentDetails> student = new List<StudentDetails>();  
  24.             while(doc!=null)  
  25.             {  
  26.                 string UNID = doc.UniversalID;  
  27.                 string Name = doc.GetItemValue("txName")[0];  
  28.                 double Age = doc.GetItemValue("txAge")[0];  
  29.                 string City = doc.GetItemValue("txCity")[0];  
  30.                 string Address = doc.GetItemValue("txAddress")[0];  
  31.                 string PhoneNo = doc.GetItemValue("txPhoneNo")[0];  
  32.                 student.Add(new StudentDetails  
  33.                 {  
  34.                     UNID=UNID,  
  35.                     StudentName=Name,  
  36.                     Age=Age,  
  37.                     City=City,  
  38.                     Address=Address,  
  39.                     PhoneNo=PhoneNo  
  40.                 });  
  41.                 doc = view.GetNextDocument(doc);  
  42.             }  
  43.             return View(student);  
  44.         }  
  45.     }  

CREATE Document
 
Now, we will see how to create a document in Lotus Notes via ASP MVC.
 
If a user clicks a Create link in list, we have to redirect a page to create form. Before clicking a create link first we have to design a form.To create a form, follow the below steps,
 
Step 1

Create new ActionResult in our controller and name it as "Create"
  1. [HttpGet]    
  2.         public ActionResult Create()    
  3.         {    
  4.             return View();    
  5.         }   
Step 2

Create a view for action. Right click on Create Action ->select Add view -> Select Template Create and Select your model ->Click OK.
 
Now, your view will be generated. Build the Application and run it.
  1. @model LotusNotesTOASPMvc.Models.StudentDetails  
  2.   
  3. @{  
  4.     ViewBag.Title = "Create";  
  5. }  
  6.   
  7. <h2>Create</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())   
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <h4>StudentDetails</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true)  
  18.   
  19.         
  20.         <div class="form-group">  
  21.             @Html.LabelFor(model => model.StudentName, new { @class = "control-label col-md-2" })  
  22.             <div class="col-md-10">  
  23.                 @Html.EditorFor(model => model.StudentName)  
  24.                 @Html.ValidationMessageFor(model => model.StudentName)  
  25.             </div>  
  26.         </div>  
  27.   
  28.         <div class="form-group">  
  29.             @Html.LabelFor(model => model.Age, new { @class = "control-label col-md-2" })  
  30.             <div class="col-md-10">  
  31.                 @Html.EditorFor(model => model.Age)  
  32.                 @Html.ValidationMessageFor(model => model.Age)  
  33.             </div>  
  34.         </div>  
  35.   
  36.         <div class="form-group">  
  37.             @Html.LabelFor(model => model.City, new { @class = "control-label col-md-2" })  
  38.             <div class="col-md-10">  
  39.                 @Html.EditorFor(model => model.City)  
  40.                 @Html.ValidationMessageFor(model => model.City)  
  41.             </div>  
  42.         </div>  
  43.   
  44.         <div class="form-group">  
  45.             @Html.LabelFor(model => model.Address, new { @class = "control-label col-md-2" })  
  46.             <div class="col-md-10">  
  47.                 @Html.EditorFor(model => model.Address)  
  48.                 @Html.ValidationMessageFor(model => model.Address)  
  49.             </div>  
  50.         </div>  
  51.   
  52.         <div class="form-group">  
  53.             @Html.LabelFor(model => model.PhoneNo, new { @class = "control-label col-md-2" })  
  54.             <div class="col-md-10">  
  55.                 @Html.EditorFor(model => model.PhoneNo)  
  56.                 @Html.ValidationMessageFor(model => model.PhoneNo)  
  57.             </div>  
  58.         </div>  
  59.   
  60.         <div class="form-group">  
  61.             <div class="col-md-offset-2 col-md-10">  
  62.                 <input type="submit" value="Create" class="btn btn-default" />  
  63.             </div>  
  64.         </div>  
  65.     </div>  
  66. }  
  67.   
  68. <div>  
  69.     @Html.ActionLink("Back to List", "Index")  
  70. </div>  
  71.   
  72. @section Scripts {  
  73.     @Scripts.Render("~/bundles/jqueryval")  

Step 3

Now, you can click the create link. It will redirect to create a form.



Step 4

We have to post the form details to controller to save the data in Lotus Notes. Create ActionResults in controller to receive the data from view. 
  1. [HttpPost]    
  2.        public ActionResult Create(StudentDetails studentDetails)    
  3.        {    
  4.            session.Initialize("password@#123");    
  5.            db = session.GetDatabase("""Migration.nsf"false);    
  6.            doc = db.CreateDocument();    
  7.            doc.ReplaceItemValue("Form""StudentInformations");    
  8.            doc.ReplaceItemValue("txName", studentDetails.StudentName);    
  9.            doc.ReplaceItemValue("txAge", studentDetails.Age);    
  10.            doc.ReplaceItemValue("txCity", studentDetails.City);    
  11.            doc.ReplaceItemValue("txAddress", studentDetails.Address);    
  12.            doc.ReplaceItemValue("txPhoneNo", studentDetails.PhoneNo);    
  13.            doc.Save(truefalse);    
  14.    
  15.            return RedirectToAction("Index""StudentInformations");    
  16.        }   
Now, you can see the saved data in ASP MVC list and same as Lotus Notes.
 

 
 
Edit document
 
To edit the document, follow the steps given below.
 
Step 1

Create an ActionResult with a parameter and name it as EDIT.
  1. [HttpGet]    
  2.         public ActionResult Edit(string UNID)    
  3.         {    
  4.              var studentDetails=new StudentDetails();    
  5.              session.Initialize("password@#123");    
  6.              db = session.GetDatabase("""Migration.nsf"false);    
  7.              doc = db.GetDocumentByUNID(UNID);    
  8.             if(doc!=null)    
  9.             {    
  10.                 studentDetails.StudentName = doc.GetItemValue("txName")[0];    
  11.                 studentDetails.Age = doc.GetItemValue("txAge")[0];    
  12.                 studentDetails.City = doc.GetItemValue("txCity")[0];    
  13.                 studentDetails.Address = doc.GetItemValue("txAddress")[0];    
  14.                 studentDetails.PhoneNo = doc.GetItemValue("txPhoneNo")[0];    
  15.             }    
  16.             return View(studentDetails);    
  17.         }   
Step 2

Create a View for action -> right click on ActionResult ->select Add View -> select Template Edit -> select your model and click OK. Now, your view will be created. Build the Application and run it.
  1. @model LotusNotesTOASPMvc.Models.StudentDetails  
  2.   
  3. @{  
  4.     ViewBag.Title = "Edit";  
  5. }  
  6.   
  7. <h2>Edit</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())  
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <h4>StudentDetails</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true)  
  18.         @Html.HiddenFor(model=>model.UNID)  
  19.         <div class="form-group">  
  20.             @Html.LabelFor(model => model.StudentName, new { @class = "control-label col-md-2" })  
  21.             <div class="col-md-10">  
  22.                 @Html.EditorFor(model => model.StudentName)  
  23.                 @Html.ValidationMessageFor(model => model.StudentName)  
  24.             </div>  
  25.         </div>  
  26.   
  27.         <div class="form-group">  
  28.             @Html.LabelFor(model => model.Age, new { @class = "control-label col-md-2" })  
  29.             <div class="col-md-10">  
  30.                 @Html.EditorFor(model => model.Age)  
  31.                 @Html.ValidationMessageFor(model => model.Age)  
  32.             </div>  
  33.         </div>  
  34.   
  35.         <div class="form-group">  
  36.             @Html.LabelFor(model => model.City, new { @class = "control-label col-md-2" })  
  37.             <div class="col-md-10">  
  38.                 @Html.EditorFor(model => model.City)  
  39.                 @Html.ValidationMessageFor(model => model.City)  
  40.             </div>  
  41.         </div>  
  42.   
  43.         <div class="form-group">  
  44.             @Html.LabelFor(model => model.Address, new { @class = "control-label col-md-2" })  
  45.             <div class="col-md-10">  
  46.                 @Html.EditorFor(model => model.Address)  
  47.                 @Html.ValidationMessageFor(model => model.Address)  
  48.             </div>  
  49.         </div>  
  50.   
  51.         <div class="form-group">  
  52.             @Html.LabelFor(model => model.PhoneNo, new { @class = "control-label col-md-2" })  
  53.             <div class="col-md-10">  
  54.                 @Html.EditorFor(model => model.PhoneNo)  
  55.                 @Html.ValidationMessageFor(model => model.PhoneNo)  
  56.             </div>  
  57.         </div>  
  58.   
  59.         <div class="form-group">  
  60.             <div class="col-md-offset-2 col-md-10">  
  61.                 <input type="submit" value="Save" class="btn btn-default" />  
  62.             </div>  
  63.         </div>  
  64.     </div>  
  65. }  
  66.   
  67. <div>  
  68.     @Html.ActionLink("Back to List", "Index")  
  69. </div>  
  70.   
  71. @section Scripts {  
  72.     @Scripts.Render("~/bundles/jqueryval")  

 Click Edit link in the list, your view will be, as shown below.
 
 
Here, we edited the Student Name "Prasad Bala" to "Prasad".

 
 
Step 4 

For update, we have to post the modified details to controller. Create a new ActionResult to receive the recrods from view. 
  1. [HttpPost]    
  2.         public ActionResult Edit(StudentDetails studentDetails)    
  3.         {    
  4.             session.Initialize("password@#123");    
  5.             db = session.GetDatabase("""Migration.nsf"false);    
  6.             doc = db.GetDocumentByUNID(studentDetails.UNID);    
  7.             if (doc != null)    
  8.             {    
  9.                 doc.ReplaceItemValue("txName", studentDetails.StudentName);    
  10.                 doc.ReplaceItemValue("txAge", studentDetails.Age);    
  11.                 doc.ReplaceItemValue("txCity", studentDetails.City);    
  12.                 doc.ReplaceItemValue("txAddress", studentDetails.Address);    
  13.                 doc.ReplaceItemValue("txPhoneNo", studentDetails.PhoneNo);    
  14.                 doc.Save(truefalse);    
  15.             }    
  16.             return RedirectToAction("Index""StudentInformations");    
  17.         }   
Now, your data will be updated in Lotus Notes.

 
 
 

Delete document
 
To delete a document, follow the steps given below.

Step 1

Create an ActionResult with the parameter and name it as Delete. 
  1. [HttpGet]    
  2.         public ActionResult Delete(string UNID)    
  3.         {    
  4.             var studentDetails = new StudentDetails();    
  5.             session.Initialize("password@#123");    
  6.             db = session.GetDatabase("""Migration.nsf"false);    
  7.             doc = db.GetDocumentByUNID(UNID);    
  8.             if (doc != null)             {    
  9.                 studentDetails.StudentName = doc.GetItemValue("txName")[0];    
  10.                 studentDetails.Age = doc.GetItemValue("txAge")[0];    
  11.                 studentDetails.City = doc.GetItemValue("txCity")[0];    
  12.                 studentDetails.Address = doc.GetItemValue("txAddress")[0];    
  13.                 studentDetails.PhoneNo = doc.GetItemValue("txPhoneNo")[0];    
  14.             }    
  15.             return View(studentDetails);    
  16.         }   
Step 2

Create a View for action -> right click on ActionResult ->Select "Add View" -> select Template Delete -> select your model and click OK. Now, your view will be created. Build the Application and run it.
  1. @model LotusNotesTOASPMvc.Models.StudentDetails  
  2.   
  3. @{  
  4.     ViewBag.Title = "Delete";  
  5. }  
  6.   
  7. <h2>Delete</h2>  
  8.   
  9. <h3>Are you sure you want to delete this?</h3>  
  10. <div>  
  11.     <h4>StudentDetails</h4>  
  12.     <hr />  
  13.     <dl class="dl-horizontal">  
  14.         <dd>  
  15.             @Html.HiddenFor(model => model.UNID)  
  16.         </dd>  
  17.   
  18.         <dt>  
  19.             @Html.DisplayNameFor(model => model.StudentName)  
  20.         </dt>  
  21.   
  22.         <dd>  
  23.             @Html.DisplayFor(model => model.StudentName)  
  24.         </dd>  
  25.   
  26.         <dt>  
  27.             @Html.DisplayNameFor(model => model.Age)  
  28.         </dt>  
  29.   
  30.         <dd>  
  31.             @Html.DisplayFor(model => model.Age)  
  32.         </dd>  
  33.   
  34.         <dt>  
  35.             @Html.DisplayNameFor(model => model.City)  
  36.         </dt>  
  37.   
  38.         <dd>  
  39.             @Html.DisplayFor(model => model.City)  
  40.         </dd>  
  41.   
  42.         <dt>  
  43.             @Html.DisplayNameFor(model => model.Address)  
  44.         </dt>  
  45.   
  46.         <dd>  
  47.             @Html.DisplayFor(model => model.Address)  
  48.         </dd>  
  49.   
  50.         <dt>  
  51.             @Html.DisplayNameFor(model => model.PhoneNo)  
  52.         </dt>  
  53.   
  54.         <dd>  
  55.             @Html.DisplayFor(model => model.PhoneNo)  
  56.         </dd>  
  57.   
  58.     </dl>  
  59.   
  60.     @using (Html.BeginForm()) {  
  61.         @Html.AntiForgeryToken()  
  62.   
  63.         <div class="form-actions no-color">  
  64.             <input type="submit" value="Delete" class="btn btn-default" /> |  
  65.             @Html.ActionLink("Back to List", "Index")  
  66.         </div>  
  67.     }  
  68. </div> 
Click Delete link in the list. Your view will be, as shown below.



You can see your selected document in your Delete view.

 

Step 3

To delete a selected record, we have to call controller to perform the delete action.
  1. [HttpPost]    
  2.        public ActionResult Delete(StudentDetails studentDetails)    
  3.        {    
  4.             session.Initialize("password@#123");    
  5.            db = session.GetDatabase("""Migration.nsf"false);    
  6.            doc = db.GetDocumentByUNID(studentDetails.UNID);    
  7.            if (doc != null)    
  8.            {    
  9.                doc.Remove(true);    
  10.            }    
  11.            return RedirectToAction("Index""StudentInformations");    
  12.        }   
Step 4

Now, your selected record will be deleted, once you click the delete button in view. Now, your updated list is shown below.



 
That's all guys. Thanks for reading. If you have any queries please write them in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all