Skip to content
Loading
    
  •     
  •                 class="p-2 rounded smallLabel border d-flex justify-content-center align-items-center">    
  •                     @message.Messenger.FirstName    
  •                     
  •                 
  •         }    
  •         
  •     
  •     
  • "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 { get; set; }    
  •     
  •         public string TheMessage { get; set; }    
  •     
  •         // one to many    
  •         public int UserID { get; set; }    
  •     
  •         public User Messenger { get; set; }
  •         public DateTime CreateAt { get; set; } = DateTime.Now;    
  •     
  •         public DateTime UpdatedAt { get; set; } = DateTime.Now;    
  •     }    
  • }    
  • +1
  • Kiran Mohanty
    +1
    Hi,
    Here is what i found. 
    1. class User {  
    2.     public int UserID { get; set; }  
    3.     public List<Recipe> MyRecipes {get;set;}  
    4.     public List<Like> MyLikes {get;set;}  
    5.     public List<Message> MyMessages {get;set;}  
    6. }  
    7.   
    8. class Recipe {  
    9.    public int UserID { get; set; }  
    10.    public User Creator { get; set; }  
    11.    public List<Like> Liked {get;set;}  
    12. }  
    13.   
    14. class Message {  
    15.    public int UserID { get; set; }  
    16.    public User Messenger { get; set; }  
    17. }  
    18.   
    19. class Like {  
    20.    public int UserID { get; set; }  
    21.    public int RecipeID { get; set; }  
    22.    public User Liker { get; set; }  
    23.    public Recipe Rec { get; set; }  
    24. }  
     This object structure creates circular reference and application fails to create JSON object . Can you please post your requirement diagram ? As per me, object structure you may need to change.
    +1
  • +1
    @Sachin Singh
    I tried that and it still did not work, like I said I wish I could zoom with you for about 20 mins just to get a real understanding of what you are doing. It was working before the Ajax call so I am confused why I needed to add the foreign key.
     
    @Kiran Mohanty 
    what do you mean by requirement diagram? I am just doing things off my mind with what and how I learned how to do this. I wish I coould zooom with you or  Singh for about 20 minutes  all my code is at my github except the few things I changed and they were in this section. 
     
    1. namespace ToledosSite.Models  
    2. {  
    3.     public class Message  
    4.     {  
    5.         [Key]  
    6.         public int MessageID { get; set; }  
    7.   
    8.         public string TheMessage { get; set; }  
    9.   
    10.         // one to many  
    11.         [ForeignKey("User")]  // the foreign Key  
    12.         public int UserID { get; set; }  
    13.   
    14.         public User Messenger { get; set; }  
    15.   
    16.   
    17.         public DateTime CreateAt { get; set; } = DateTime.Now;  
    18.   
    19.         public DateTime UpdatedAt { get; set; } = DateTime.Now;  
    20.     }  
    21. }  
     then 
    1.   
    2. class="m-4">// this was originally not a partial  
    3.    "PartialMessage">  
    4.   
    then my controller I basically only changed 
    1. [HttpPost("NewMessage/Add")]  
    2. / this was originally public IActionResult  
    3.        public JsonResult NewMessage( Message newMessage)  
    4.        {  
    5.          
    6.            User userInDB = GetUserInDB();  
    7.            Message thisMessage = newMessage;  
    8.            thisMessage.UserID = userInDB.UserID;  
    9.             
    10.            DBContext.Messages.Add(thisMessage);  
    11.            DBContext.SaveChanges();  
    12.   
    13.            return Json(thisMessage);  
    14.        }  
    my orginal code is on my github https://github.com/althepal78/melskitchen
    +1
  • +1
    so if you want to know what I am trying to do here is my question in greater detailed in stackoverflow because someone tried to help me there and it didn't work it gives me an alert. I don't want a freaking a lert I am sorry I have ptsd and this is driving me crazy 
     
     
     https://stackoverflow.com/questions/65815325/how-to-use-ajax-with-mvc-mysql-and-entity-framework-no-razor-pages?
     
    So the information on stackoverflow is doing this to me at this time I made a video about it
    https://youtu.be/KEub5IdYkkQ 
    +1
  • +1
    I got it to work Amen
     
    here is the answer on stack overflow
     
    https://stackoverflow.com/questions/65815325/how-to-use-ajax-with-mvc-mysql-and-entity-framework-no-razor-pages/65816660?noredirect=1#comment116604642_65816660 
    +1
  • Sachin Singh
    0
    these are the steps
    1. public void ConfigureServices(IServiceCollection services)  
    2. {  
    3.     services.AddMvc()  
    4.             .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());  
    5. } 
     
    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.     }  
    19. } 
     
    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.      
    0
  • Sachin Singh
    0
    one to many relationship is not configured correctly.
    1. namespace ToledosSite.Models    
    2. {    
    3.     public class Message    
    4.     {    
    5.         [Key]    
    6.         public int MessageID { get; set; }    
    7.     
    8.         public string TheMessage { get; set; }    
    9.     
    10.        [ForeignKey("User")]  
    11.         public int UserID { get; set; }    
    12.     
    13.         public User Messenger { get; set; }    
    14.     
    15.     
    16.         public DateTime CreateAt { get; set; }    
    17.     
    18.         public DateTime UpdatedAt { get; set; }   
    19.     }    
    20. }   
     
    0