May be this sounds very stupid question but I'm a student who just came to know about MVC and I find it very interesting. Also, I have very little or no knowledge about Ajax and jQuery. I have been creating some basic applications like posting a comment or a blog post without using Ajax services or jquery. Now I've come to a part where I see Ajax services being called and custom jQuery code are being written.
Let us consider a small example where one can add comment on the main page and after the comment being written and submitted, it appears on main page. Imagine a controller with following functions
public class CustomAjaxController : Controller
{
//
// GET: /CustomAjax/
private static readonly List<string> Comments = new List<string>();
public ActionResult Index()
{
return View(Comments);
}
[HttpPost]
public ActionResult AddComment(string comment)
{
Comments.Add(comment);
return RedirectToAction("Index");
}
}
I know using list of string to store the comments is not good idea but for the sake of simplicity and to explain my point I've used it. Also I don't have model but again for same reason.
@model IEnumerable<string>
<h4>comments</h4>
<ul id ="comments">
@foreach (var comment in Model)
{
<li>@comment</li>
}
</ul>
<form method="POST" id="commentsForm"
action = "@Url.Action("AddComment")">
@Html.TextArea("Comment", new{rows =5, cols =40})
<br/>
<input type ="submit" value="Add comment"/>
</form>
Now, I've seen same thing being done using jquery and ajax requests. But why should i do it to achieve same result, or how do I know this is the right place to use ajax requests.