I want to add popups in which user can add comments for each grid on web. I want to add this comment to database and close popup without refreshing main page.
How can I do it? Here is my code.
$('a.dialog').click(function () {
var x = jQuery(this).position().left + jQuery(this).outerWidth();
var y = jQuery(this).position().top - jQuery(document).scrollTop();
$.get(
this.href,
function (result) {
$(result).dialog({
modal: true,
width: 500,
position: [x, y]
});
}
);
return false;
});
Here is Post from controller:
[HttpPost]
public ActionResult Comment(CommentsModel model)
{
try
{
model.UserId = Storage.UserGetActive().Id;
Storage.CommentInsert(model);
return RedirectToAction("Index");
}
catch (Exception e)
{
return RedirectToAction("Error", e);
}
}
I know I’m doing it wrong. How can I just save comment and close popup?
EDIT
I’m just making link to it, like this:
<a class="dialog" href="/Dashboard/Comment/'+clips[i-1]+'">Comment</a>
This is what I have in my view:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Add new comment</legend>
@Html.HiddenFor(m => m.MetriceId)
<div>
@Html.LabelFor(m => m.Comment)
</div>
<div >
@Html.EditorFor(m => m.Comment, new { style = "width:450px; height:70px" })
@Html.ValidationMessageFor(m => m.Comment)
</div>
<p>
<input type="submit" value="Save Comment" />
</p>
</fieldset>
}
Firstly update your action method so that it is not redirecting and simply returns a status:
You need to set up your dialog to post to your action. See JQuery help
Firstly add html to your page for your dialog to exist
Secondly initialise you dialog:
Lastly you need to set up your links to open the dialog:
The above should be enough to give you a pop up. Note, this is not your full solution
I would recommend reading through the jQuery docs on modals and posts: