【问题标题】:How to Update TextBox in MVC Ajax如何在 MVC Ajax 中更新文本框
【发布时间】:2011-02-14 23:24:33
【问题描述】:

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

我是 MVC Ajax 应用程序的新手。请任何人帮助我。

谢谢, 庞库马尔潘迪安.T

【问题讨论】:

    标签: ajax model-view-controller


    【解决方案1】:

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

    【讨论】:

    • 其实你错了。如果你使用 UpdateTargetId ,它会替换整个innerHtml。这是做不到的。或许您可以分享一些示例代码,以便更好地理解您的答案。
    • 是的,只需再次返回带有新值的文本框。最好把它渲染成局部的,不要重复自己。
    • 我觉得你穿了。我将发布我尝试过的这个问题的答案。
    • 这不是最好的方法,但它是最简单的方法并且有效!当然,您可以在控制器中返回 json,然后通过 jQuery 更新文本框值。
    【解决方案2】:

    我们不能直接在 UpdateTargetId 属性中给文本框控件 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")%>
    

    谢谢, 庞库马尔潘迪安.T

    【讨论】:

      【解决方案3】:

      您可以使用

      来实现这个简单的概念

      1- 发布\获取

      2- 阿贾克斯

      注意:只需将 $("#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";
          }
      

      //// 在视图中添加对 Javascript (jQuery) 文件的引用

      @section Scripts{
      
      <script src="~/Scripts/modernizr-2.6.2.js"></script>
      <script src="~/Scripts/jquery-1.8.2.intellisense.js"></script>
      <script src="~/Scripts/jquery-1.8.2.js"></script>
      <script src="~/Scripts/jquery-1.8.2.min.js"></script>  
      }
      

      /// 然后添加 Post 方法如下:

      <script type="text/javascript"> 
      
      /// post and get text value
      
      $("#send").on("click", function () {    
      $.post('', { message: $('#msg').val() })  
      
      //// empty post('') means post to the default controller, 
      ///we are not pacifying different action or controller
      /// however we can define a url as following:
      /// var url = "@(Url.Action("GetDataAction", "GetDataController"))"
      
               .done(function (response) {
                   $("#myform").html(response);
              })
              .error(function () { alert('Error') })
              .success(function () { alert('OK') })
              return false;
          }); 
      
         </script>
      

      2-你也可以使用 Get only,如下所示:

         /////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>
      

      3- 现在假设您想使用 $.Ajax 和 JSON 来实现:

      // 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);  
          }
      

      然后,在您的视图中:添加事件单击输入类型按钮,甚至是来自提交: 只需确保您的 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>
      

      干杯,

      马哈茂德·赛义德

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-14
        • 1970-01-01
        • 2018-12-16
        相关资源
        最近更新 更多