Skip to content
Loading
Dotnet Core, Ajax and Databases    
  •                 
  •         }    
  •         
  •     
  •     
  • "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">    
  •     
  • @* just a space to know I am starting the ajax call *@    
  •     
  • class="m-4">    
  •    "PartialMessage">    
  •     
  • /// the partial message    
  •  @model Message    
  •      
  •  @if (ViewBag.User != null)    
  •     {    
  •         "myForm" asp-action="NewMessage" asp-controller="Home"  a method="post">    
  •             class="form-control-lg inputLabel m-1 " asp-for="TheMessage">    
  •             class="btn btn-outline-primary btn-warning m-2" type="submit">Send    
  •             
  •     }    
  •     
  • /// the model    
  • using System.ComponentModel.DataAnnotations;    
  • using System;    
  •     
  • namespace ToledosSite.Models    
  • {    
  •     public class Message    
  •     {    
  •         [Key]    
  •         public int MessageID { getset; }    
  •     
  •         public string TheMessage { getset; }    
  •     
  •         // one to many    
  •         public int UserID { getset; }    
  •     
  •         public User Messenger { getset; }
  •         public DateTime CreateAt { getset; } = DateTime.Now;    
  •     
  •         public DateTime UpdatedAt { getset; } = DateTime.Now;    
  •     }    
  • }    
  • I wish I could message you man so I can ask you a few questions of where I would do some stuff because I tried some stuff just getting lost
  • Sachin Singh
    even if you don't add the jsonOption then also the ajax will work , so simply issue an ajax get or post request from view to controller as i described in the example at it should work.
  • I dont have options.serializer.contractresolver in my setup so this does not  work for me I get unresolve information
  • @Sachin Singh, does it matter what MVC I am doing? Like is this always the way even when you trying to get the information from a database?
  • Sachin Singh
    these are the steps
    1. public void ConfigureServices(IServiceCollection services)  
    2. {  
    3.     services.AddMvc()  
    4.             .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());  

     
    1. public class HomeController : Controller  
    2. {  
    3.     // GET: Home  
    4.     public IActionResult Index()  
    5.     {  
    6.         return View();  
    7.     }  
    8.    
    9.     [HttpPost]  
    10.     public JsonResult AjaxMethod(string name)  
    11.     {  
    12.         PersonModel person = new PersonModel  
    13.         {  
    14.             Name = name,  
    15.             DateTime = DateTime.Now.ToString()  
    16.         };  
    17.         return Json(person);  
    18.     }  

     
    1. "text" id="txtName"/>  
    2.     "button" id="btnGet" value="Get Current Time"/>  
    3.     "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  
    4.     "text/javascript">  
    5.         $(function () {  
    6.             $("#btnGet").click(function () {  
    7.                 $.ajax({  
    8.                     type: "POST",  
    9.                     url: "/Home/AjaxMethod",  
    10.                     data: { "name": $("#txtName").val() },  
    11.                     success: function (response) {  
    12.                         alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);  
    13.                     },  
    14.                     failure: function (response) {  
    15.                         alert(response.responseText);  
    16.                     },  
    17.                     error: function (response) {  
    18.                         alert(response.responseText);  
    19.                    }  
    20.                 });  
    21.             });  
    22.         });  
    23.