How to make a partial record in Asp.Net MVC?

I am new to asp.net mvc.

I want to create a site that allows the visitor to make a partial entry, for example, allow visitors to click the like button to vote for a comment.

How to do this in asp.net mvc?

+4
source share
2 answers

You can implement this using Ajax , the browser will send a message "behind the scenes", so to speak, without redirecting the user, the Server will return the data in JSON format .

On the server: create a new CommentsController and add Action Like :

 [Authorize] /*optional*/ public JsonResult Like(int id) { //validate that the id paramater //Insert/Update the database return Json(new {result = true}); } 

In your opinion, just use jQuery Ajax methods:

 function likeComment(id) { $.post('<%=Url.Action("Like", "Comments")%>/' + id, function(data){ //Execute on response from server if(data.result) { alert('Comment liked'); } else { alert('Comment not liked'); } }); } 
+9
source

ASP.Net MVC is not limited to use only one form per page, such as a Web Form. While an Ajax solution is preferable to your scenario, you can also use regular HTTP POST , as shown below;

 @using (Html.BeginForm(new { controller = "Comments", action = "Like" })) { <button type="submit">Like</button> } 
+1
source

Source: https://habr.com/ru/post/1339772/


All Articles