Herlan

Herlan

  • NA
  • 215
  • 2.9k

Passing value from MVC View to Controller using ajax

Jan 28 2021 3:35 PM
Hi!
I am developing web app with asp.net core 3.1.
I have an ajax call sends a ConsultViewModel object to my controller, but in controller it is getting null.
How do i solve this problem?
 
My code is just as follow:
 
  1. public class ConsultViewModel  
  2. {  
  3.         public int Id { getset; }  
  4.         [Range(1, int.MaxValue, ErrorMessage = "Select a Department")]  
  5.         public int DepartmentId { getset; }  
  6.         [Required]  
  7.         public string Description { getset; }  
  8.         public string Date { getset; }  
  9.         public IEnumerable<SelectListItem> Department { getset; }  
  10. }  
  1.         @model MyApp.AppWeb.Models.ConsultViewModel;  
  2.   
  3.         <h1>Edit</h1>  
  4.         <form asp-action="Edit">  
  5.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  6.             <input type="hidden" asp-for="Id" />  
  7.             <div class="form-group">  
  8.                 <label asp-for="DepartmentId" class="control-label"></label>  
  9.                 <select asp-for="DepartmentId" asp-items="Model.Department" class="form-control"></select>  
  10.                 <span asp-validation-for="DepartmentId" class="text-danger"></span>  
  11.             </div>  
  12.             <div class="form-group">  
  13.                 <label asp-for="Date" class="control-label"></label>  
  14.                 <input asp-for="Date" class="form-control" disabled />  
  15.                 <span asp-validation-for="Date" class="text-danger"></span>  
  16.             </div>  
  17.             <div class="form-group">  
  18.                 <label asp-for="Description" class="control-label"></label>  
  19.                 <textarea asp-for="Descriptionion" class="form-control" cols="40" rows="5"></textarea>  
  20.                 <span asp-validation-for="Descritcioncion" class="text-danger"></span>  
  21.             </div>  
  22.             <div id="@Model.Id" class="form-group">  
  23.                 <input type="button" class="btn btn-primary" value="Save" id="btnEdit"/>  
  24.             </div>  
  25.         </form>  
  1. @section Scripts {  
  2.     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  3.   
  4.     <script type="text/javascript">  
  5.         $(document).ready(function () {  
  6.             $('#btnEdit').click(function () {  
  7.                 var id = $(this).parent()[0].id;  
  8.                 var consultView = {  
  9.                     Id: id,  
  10.                     DepartmentId: $('#DepartmentId').val(),  
  11.                     Date: $('#Date').val(),  
  12.                     Description: $('#Description').val()  
  13.                 };  
  14.   
  15.                 $.ajax({  
  16.                     type: 'POST',  
  17.                     url: "@Url.Action("Edit")",  
  18.                     contentType: "application/json; charset=utf-8",  
  19.                     dataType: 'json',  
  20.                     data: JSON.stringify({consultView: consultView}),   
  21.                     success: function (data) {  
  22.                         alert(data.message);  
  23.                     },  
  24.                     error: function (ex) {  
  25.                         alert('Error.' + ex);  
  26.                     }  
  27.                 });  
  28.             });  
  29.         });  
  30.     </script>  
  31. }  
  1.         [HttpPost]  
  2.         //[ValidateAntiForgeryToken]  
  3.         public async Task<IActionResult> Edit(ConsultViewModel consultView)  
  4.         {  
  5.             if (ModelState.IsValid)  
  6.             {  
  7.                 //More code..  
  8.   
  9.                 return Json(new { success = true, message = "Done." });                 
  10.             }  
  11.   
  12.             return View(consultaView);  
  13.         }  
 Thanks!
 

Answers (3)