Skip to content
Loading
Edit & Add using Partial View  
  •                   
  •               
  •           
  •     }  
  •  step 2. Modify the call as
     
    1. $('body').on('click', '[id*=btnAdd]', function () {    
    2.   $.get("/Controller/Add",function(myHtml){    
    3. $(".modal-body").Html(myHtml);    
    4. )    
    5.        $(".modal-title").html("Add new location");    
    6.     
    7.        $('#myModal').modal('show');    
    8.    });    
    9.     
    10.    $(document).on('click', '[id*=btnEdit]', function () {   
    11. $.get("/Controller/Edit",function(myHtml){  
    12. $(".modal-body").Html(myHtml);)  
    13.   $(".modal-title").html("Update");    
    14.        $('#myModal').modal('show');    
    15.    });    
    16.    
     
     step 3. Modify the partial view to show the value as
     
    1. @model MyApplication.Models.Location  
    2.   
    3.   
    4.       
    5.           
    6.         class="form-group">  
    7.             @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-3" })  
    8.             class="col-md-5">  
    9.                 @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" @Value="Model.Id" } })  
    10.                 @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })  
    11.             
      
  •         
  •   
  •         // similarly add value field for all  
  •               
  •           
  •       
  •   
  •  
    step 4. Create two methods in the controller as
    1. public ActionResult Add()  
    2. {  
    3. return PartialVew("_CreateEdit",new Product())  
    4. }  
    1. public ActionResult Edit(int productId)    
    2. {    
    3. var product= Context.Products.Where(x=>x.Id==productId).Single();  
    4. return PartialVew("_CreateEdit",product)    
    5. }    
    +1