After going through a tutorial on creating a basic messaging web application using SignalR and MVC, I realized that the tutorial did not cover how to store messages in a database or display previous messages to users. The main code snippet from the tutorial looks like this:
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
...
With an EF Code-first setup where there is a Comment class defined as:
public class Comment
{
public int CommentID { get; set; }
public string UserName { get; set; }
public string CommentText { get; set; }
}
And a corresponding Comment controller with a Create method:
public ActionResult Create([Bind(Include = "CommentID, UserName, CommentText")] Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(comment);
}
I understand that I need to incorporate AJAX functionality to fetch and post messages to the database. However, I am unsure about how to go about it. Can someone guide me on writing the necessary AJAX queries to achieve this? Any help or suggestions would be greatly appreciated. Thank you.