如何获得用户输入Ajax.ActionLink呼吁采取行动的方法? [英] How to get user input for Ajax.ActionLink call for action method?

查看:112
本文介绍了如何获得用户输入Ajax.ActionLink呼吁采取行动的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想用ajax操作链接保存一个字段与看法没有改变。

Suppose I want to use ajax action link to save one field with no change for view.

下面是我的看法(它是绑定到类型的模型一个强类型的视图):

Here is my view(it is a strongly-typed view bound to type Model):

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

下面是我在阿贾克斯的控制器ActionLink的行动:

Here is my Action for ajax actionlink in 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;
    }          
}

问题是:我不能让用户输入文本中的Mycomment的操作方法。不管用户输入的浏览器Mycomment,当点击ActionLink的阿贾克斯保存,然后我检查从Mycomment参数模型的价值,没有什么改变MyComment。

The problem is: I can't get the user input in textbox for Mycomment in action method. Whatever user input in browser for Mycomment, when click on Ajax ActionLink SAVE, then I check the value from param model for Mycomment, there is nothing changed for MyComment.

困惑的是:模型应绑定到查看双向的方式。但这种情况下,似乎不。
然后,我改变了Ajax.ActionLink调用:

Confused is: Model should be bound to view in bi-way. 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"})

再改操作方法签名:

then changed action method signature as:

public void SaveComment(string commentinput)

我仍然不能得到用户的输入。

I still can't get the user input.

如何获得在控制器的操作方法,用户输入?

How to get the user input in controller action method?

推荐答案

AFAIK Ajax.ActionLink不会让你轻松获取表单值和AJAX张贴。还有一些变通这似乎相当hackish的(看着最后段落的适应的动态URL 的让我哭)。

AFAIK Ajax.ActionLink doesn't allow you to easily get form values and post them with AJAX. There are some workarounds which seem quite hackish (looking at the last paragraph Adapting the URL Dynamically makes me cry).

下面是jQuery的方式(我preFER,如果相比于MS阿贾克斯):

Here's the jQuery way (I prefer it if compared to MS Ajax):

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

然后悄悄地附上一个Click事件处理到锚:

And then unobtrusively attach a click event handler to the anchor:

$(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();
    });
});

这篇关于如何获得用户输入Ajax.ActionLink呼吁采取行动的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆