I have following controllers and it works properly for _GetForSession
and CommentForm
controllers. but when hit the _Submit
parameter comment
object is null. My controller class as follows:
public class CommentController : Controller
{
//
// GET: /Comment/
public ActionResult Index()
{
return View();
}
public PartialViewResult _GetForSession(string isbnNo )
{
ViewBag.ISBN_No = isbnNo;
List<CommentModel> comments = CommentFacade.GetAllCommentsOnIsbn(isbnNo);
return PartialView("_GetForSession", comments);
}
[ChildActionOnly]
public PartialViewResult _CommentForm(string isbnNo)
{
CommentModel comment = new CommentModel() { ISBN_No = isbnNo };
return PartialView("_CommentForm", comment);
}
[ValidateAntiForgeryToken]
public PartialViewResult _Submit(CommentModel comment)
{
CommentFacade.SaveComment(comment);
List<CommentModel> comments = CommentFacade.GetAllCommentsOnIsbn(comment.ISBN_No);
ViewBag.ISBN_No = comment.ISBN_No;
return PartialView("_GetForSession", comments);
}
}
My Views are as follows:
view -_GetForSession
@model IEnumerable<LibraryManagementWeb.Models.CommentModel>
<div id="comments">
<ul>
@foreach (var comment in Model)
{
<li>@comment.Comment</li>
}
</ul>
@using (Ajax.BeginForm("_Submit", "Comment", new AjaxOptions() { UpdateTargetId="comments"}))
{
@Html.AntiForgeryToken()
@Html.Action("_CommentForm", new { isbnNo= ViewBag.ISBN_No })
}
</div>
view - _CommentForm
@model LibraryManagementWeb.Models.CommentModel
<h2>_CommentForm</h2>
@Html.HiddenFor(model => model.ISBN_No)
<div>
@Html.EditorFor(model => model.ISBN_No)
<br />
@Html.LabelFor(model => model.Comment)
@Html.EditorFor(model => model.Comment)
</div>
<button type="submit">Submit Comment</button>
I tried every possible things, but couldn't find solution for this. What I missed in here?
Edit: fiddler out put:
fiddler raw view is as follows:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Account/Login?ReturnUrl=%2fBook%2fDetails%2f7">here</a>.</h2>
</body></html>