Skip to content
Loading
Create & Update
0
  • Kirtesh Shah
    See below article for Data Annotation at client side
     
    https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/enabling-client-side-validation-on-custom-data-annotations-w/ 
     
    Also see below article for Create and Update Partial view
     
    https://oakdome.com/programming/MVC_Inserting&Updating_with_Partial_Views.php 
     
    Hope it will help you 
    +2
  • Sachin Singh
    step 1. Create one partial view ("_UpSert") as shown below, just copy and paste.
    1. @model YourprojectName.Models.Customer  
    2.   

    3.                 class="modal" tabindex="-1" role="dialog" id="UpSertModal">  
    4.                     class="modal-dialog" role="document">  
    5.                         class="modal-content">  
    6.                             class="modal-header">  
    7.                                 @*"button" class="close" data-dismiss="modal">"true">×class="sr-only">Close*@  
    8.                                 class="modal-danger">Delete Confirmation  
    9.                             
      
  •                             class="modal-body">  
  •     @using (Html.BeginForm("UpSert", "Controller", FormMethod.Post)){
  • @Html.AntiForgeryToken()
  • @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  •   
  • //@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })  
  •   
  • //@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })  
  • "hidden" Name="Id" value="@Model.Id"/>  
  •   
  • @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })  
  •   
  • @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })  
  •   
  • @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", @Value="Model.Description" } })  
  •   
  • @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })  
  •   
  • @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })  
  •   
  • @Html.CheckBoxFor(model => model.IsActive,new{@checked=(Model.IsActive==true?"checked":"")})  
  •   
  • @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })  
  • }
  •                               
  •                             class="modal-footer">  
  •                                 "button" class="btn btn-secondary" data-dismiss="modal">Cancel  
  •                                 "submit" class="btn btn-danger">Delete  
  •                               
  •                           
  •                       
  •                   
  •              
  •  step 2.  Create a method to return the above partial view
    1. public ActionResult GetPartialView(int Id=0)  
    2. {  
    3. if(Id==0)  
    4. {  
    5. return PartialView("_UpSert",new Customer());  
    6. }  
    7. else  
    8. {  
    9. Customer obj= new Customer();  
    10. using(SqlConnection con= new SqlConnection("constring"))  
    11. {  
    12. SqlCommand cmd= new SqlCommand("select 8 from Customer where Id=@Id");  
    13. cmd.Parameters.AddWithValue("@Id",Id);  
    14. con.Open();  
    15. SqlDataReader rdr= cmd.ExecuteReader();  
    16.   
    17. While(rdr.Read())  
    18. {  
    19. obj.Id= Convert.ToInt32(rdr[0]);  
    20. obj.Description= rdr[1].ToString();  
    21. obj.IsActive= Convert.ToBoolean(rdr[1]);  
    22.   
    23. }  
    24.   
    25. }  
    26. return PartialView("_UpSert",obj)  
    27. }  
    28.   
    29. }  
     step 3. On Add Click
    1. $("#Add").Click(function(){  
    2. $.get("/Controller/GetPartialView", function(data){  
    3. $("#AnyDiv").Html(data);  
    4. $("#UpSertModal").modal('open');  
    5. });  
    6.   
    7.   
    8. });  
     step 4. On Edit click
     
    1. $("#Edit").Click(function(){    
    2. var id= $(this).attr("data-Id");  
    3. $.get("/Controller/GetPartialView?Id="id, function(data){    
    4. $("#AnyDiv").Html(data);    
    5. $("#UpSertModal").modal('open');    
    6. });    
    7.     
    8.     
    9. });    
    0