How to get user input to call Ajax.ActionLink for action?

Suppose I want to use the ajax action reference to keep a single field unchanged for presentation.

Here is my view (this is a strongly typed view associated with the Model type):

   <%= Html.TextBox("myComment", Model.MyComment)%>
   <%= Ajax.ActionLink("SAVE", "SaveComment", "Home",
       Model, new AjaxOptions { HttpMethod = "POST"})
   %>

Here is my action for ajax actionlink in the controller:

[AcceptVerbs(HttpVerbs.Post)]
public void SaveComment(Model model)
{
    MyRepository repository = new MyRepository();
    Model mod = repository.GetModel(model.ID);
    mod.MyComment = model.MyComment;

    try
    {
        repository.Save();
    }
    catch(Exception ex)
    {
        throw ex;
    }          
}

The problem is that I cannot get user input in the text box for Mycomment in the action method. No matter which user enters the browser for Mycomment, when I click on Ajax ActionLink SAVE, I check the value from the param model for Mycomment, nothing has changed for MyComment.

Confused is: The model must be tied to view in two directions. but for this case, it seems not. Then I changed the Ajax.ActionLink call to:

Ajax.ActionLink("SAVE", "SaveComment", "Home",
       new {commentinput =Model.MyComment, new AjaxOptions { HttpMethod = "POST"})

:

public void SaveComment(string commentinput)

.

?

+3
1

AFAIK Ajax.ActionLink AJAX. , ( . URL- ).

jQuery ( MS Ajax):

<%= Html.TextBox("myComment", Model.MyComment) %>
<%= Html.ActionLink("SAVE", "SaveComment", "Home", new { id = "saveComment" }) %>

:

$(function() {
    $('a#saveComment').click(function(evt) {
        $.ajax({
            url : this.href,
            data: {
                // Assuming your model has a property called MyComment
                myComment: $('#myComment').val()
            },
            success: function(data, textStatus) {
                // Comment saved successfully
            }
        });
        evt.preventDefault();
    });
});
+4

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


All Articles