如何在MVCAJAX中更新文本框

如何在MVCAJAX中更新文本框,ajax,model-view-controller,Ajax,Model View Controller,如何使用MVC Ajax UpdateTargetId选项更新文本框 我不熟悉MVCAJAX应用程序。请任何人帮帮我 谢谢, Pon Kumar Pandian.T您可以简单地将所有您想要更新的内容包装在某个div中,id与您放入UpdateTargetId属性中的id相同,并返回新内容(具有新值的新文本框)。另外,不要忘记将Ajax更新类型设置为替换(而不是追加)。我们不能在UpdateTargetId属性中直接给出textbox控件id。但我们可以为此而努力。请对同样的代码进行罚款 //一旦

如何使用MVC Ajax UpdateTargetId选项更新文本框

我不熟悉MVCAJAX应用程序。请任何人帮帮我

谢谢,
Pon Kumar Pandian.T

您可以简单地将所有您想要更新的内容包装在某个div中,id与您放入UpdateTargetId属性中的id相同,并返回新内容(具有新值的新文本框)。另外,不要忘记将Ajax更新类型设置为替换(而不是追加)。

我们不能在UpdateTargetId属性中直接给出textbox控件id。但我们可以为此而努力。请对同样的代码进行罚款

//一旦ajax请求成功,就可以使用回调方法

function fnOnSuccess(context) {

        alert(context); //this context having all the current ajax req & res information.

        var response = context.get_data(); //Using this get_data() method we can get the response from server.

        $('#txtajaxResponse')[0].value = response;    
    }


<!-- Code in .aspx page -->
<%=Ajax.ActionLink("TextBox in UpdateTargetId","GetInfo","Ajax",new AjaxOptions{OnSuccess = "fnOnSuccess"}) %>

<%=Html.TextBox("txtajaxResponse")%>
函数fnOnSuccess(上下文){
警报(上下文);//此上下文包含所有当前ajax req&res信息。
var response=context.get_data();//使用这个get_data()方法,我们可以从服务器获取响应。
$('#txtajaxsresponse')[0].value=response;
}
谢谢,
Pon Kumar Pandian.T

您可以使用

1-发布\获取

2-Ajax

注意:只需将$(“#myform”).html()替换为$(“#mytext”).text=“data”

1-使用Get\Post

/////// Controller post and get simple text value 
[HttpPost]
    public string Contact(string message)
    { 
        return "<h1>Hi,</h1>we got your message, <br />" + message + " <br />Thanks a lot";
    }
///他认为:

 $("#getfullname").on("click", function () {
        var url = "@(Url.Action("FullName", "Home"))"
            $.get(url, {   })
            .done(function (response) {
                $("#myform").html(response)
            })
            .error(function () { alert('Error') })
            .success(function () { alert('OK') })
            return false;
        });
   </script>
然后,在您的视图中:添加事件单击输入类型按钮,甚至单击来自提交的: 只需确保JSON数据格式正确即可

  $("#jsonGetfullname").on("click", function () { 
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "@(Url.Action("JsonFullName", "Home"))",
            data: "{ \"fname\" : \"Mahmoud\" , \"lastname\" : \"Sayed\" }",
            dataType: "json",
            success: function (data) {
                var res = $.parseJSON(data);
                $("#myform").html("<h3>Json data: <h3>" + res.fname + ", " + res.lastname)
            }, 
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            } 
        })
    });

   </script>
$(“#jsonGetfullname”)。在(“单击”,函数(){
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“@(url.Action(“JsonFullName”,“Home”)”,
数据:“{\“fname\”:“Mahmoud\”,“lastname\”:“Sayed\”}”,
数据类型:“json”,
成功:功能(数据){
var res=$.parseJSON(数据);
$(“#myform”).html(“Json数据:“+res.fname+”,“+res.lastname”)
}, 
错误:函数(xhr,err){
警报(“readyState:+xhr.readyState+”\n状态:+xhr.status);
警报(“responseText:+xhr.responseText”);
} 
})
});
干杯


Mahmoud说,实际上你错了。如果你使用UpdateTargetId,它将替换整个innerHtml。这是不可能做到的。也许您可以分享一些示例代码,以便更好地理解您的答案。是的,只需再次返回带有新值的文本框。最好把它放在不重复的地方。我想你的工作。我会把我试过的这个问题的答案贴出来。这不是最好的方法,但它是最简单的方法,而且很有效!当然,您可以在控制器中返回json,然后通过jQuery更新textbox值。
   /////Controller = Home
   //// Action = FullName
   ///// get only simple text message
    [HttpGet]
    public string FullName()
    { 
        return "full Name: Mahmoud Sayed";
    }
 $("#getfullname").on("click", function () {
        var url = "@(Url.Action("FullName", "Home"))"
            $.get(url, {   })
            .done(function (response) {
                $("#myform").html(response)
            })
            .error(function () { alert('Error') })
            .success(function () { alert('OK') })
            return false;
        });
   </script>
// Post JSON data  add using System.Net;
    [HttpPost]
    public JsonResult JsonFullName(string fname, string lastname)
    {
        var data = "{ \"fname\" : \"" + fname  + " \" , \"lastname\" : \"" + lastname + "\" }";
//// you have to add the JsonRequestBehavior.AllowGet 
 //// otherwise it will throw an exception on run-time.
        return Json(data, JsonRequestBehavior.AllowGet);  
    }
  $("#jsonGetfullname").on("click", function () { 
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "@(Url.Action("JsonFullName", "Home"))",
            data: "{ \"fname\" : \"Mahmoud\" , \"lastname\" : \"Sayed\" }",
            dataType: "json",
            success: function (data) {
                var res = $.parseJSON(data);
                $("#myform").html("<h3>Json data: <h3>" + res.fname + ", " + res.lastname)
            }, 
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            } 
        })
    });

   </script>