使用jquery刷新页面的一部分

使用jquery刷新页面的一部分,jquery,ajax,asp.net-mvc,razor,Jquery,Ajax,Asp.net Mvc,Razor,我有一个页面,该html的一部分在其中。我希望使用ajax的用户调用我的代码,然后在我的代码会话中进行更新,然后我希望使用jquery更新此表。除了使用jquery更新表之外,我所有的操作都是用jquery完成的。我该怎么做 <table> <tr> <td>Space</td> <td>@Html.TextBox("BenchSpace", "", new { maxlength = 20, style = "width:80

我有一个页面,该html的一部分在其中。我希望使用ajax的用户调用我的代码,然后在我的代码会话中进行更新,然后我希望使用jquery更新此表。除了使用jquery更新表之外,我所有的操作都是用jquery完成的。我该怎么做

    <table>
<tr>
<td>Space</td>
<td>@Html.TextBox("BenchSpace", "", new { maxlength = 20, style = "width:80px;" })</td>
<table id="tblBench">
        <tr >
            <th >space</th>
            <th ></th>
         </tr>
            @if (Session["BenchList"] != null)
             {
                var BenchList= Session["BenchList"] as List<Common.Mines.Entities.Bench>;
                  foreach (var b in BenchList)
                   {
                      <tr class='tRow'>
                          <td class='tbody'>@b.BenchSpace </td>
                          <td class='tbody'><a href='#' title='Del' ></a>  </td>
                      </tr>
                     }
                  }
           </table>
         </tr>
         <tr>
           <td>
            <a href="#" id="InsertBench" class="insertBtn">Add</a>
           </td>
        </tr>
</table>
      $(document).on('click', "#InsertBench", function (e) {
          e.preventDefault();
          var url = '@Url.Action("SetBenchList")';
          var BenchSpace = ($.trim($('#BenchSpace').val()));
          $.post(url, { BenchSpace: BenchSpace }, function (data) {
                  if (data == "True") {
                    //how to update tblBench
                  }
              });
      });
//控制器

  public bool SetBenchList(decimal BenchSpace, bool ActionType)
        {
            var BenchList = new List<Common.Mines.Entities.Bench>();

            if (Session["BenchList"] != null)
                BenchList = Session["BenchList"] as List<Common.Mines.Entities.Bench>;

                BenchList.Add(new Common.Mines.Entities.Bench() { BenchSpace = BenchSpace });

                Session["BenchList"] = BenchList;
                return true;
            }
}

您所需要做的就是创建一个ajax请求,然后用新数据更新表的内容

你可以从这个开始

$.ajax({
  url: 'the_page's_url.cshtml'
  success: function (data) {
   $('#tableid').html(data);
  }
});

现在,ajax请求返回的数据将写入表中。您需要确保表内容正常

我使用局部视图

例如:

public PartialViewResult SetBenchList(decimal BenchSpace, bool ActionType)
        {
            var BenchList = new List<Common.Mines.Entities.Bench>();

            if (Session["BenchList"] != null)
             {
                BenchList = Session["BenchList"] as List<Common.Mines.Entities.Bench>;

                BenchList.Add(new Common.Mines.Entities.Bench() { BenchSpace = BenchSpace });

                Session["BenchList"] = BenchList;

            }
            return PartialView();
      }
你的部分观点:

@if (Session["BenchList"] != null)
             {
                var BenchList= Session["BenchList"] as List<Common.Mines.Entities.Bench>;
                  foreach (var b in BenchList)
                   {
                      <tr class='tRow'>
                          <td class='tbody'>@b.BenchSpace </td>
                          <td class='tbody'><a href='#' title='Del' ></a>  </td>
                      </tr>
                     }
                  }
$(document).on('click', "#InsertBench", function (e) {
          e.preventDefault();
          var url = '@Url.Action("SetBenchList")';
          var BenchSpace = ($.trim($('#BenchSpace').val()));
          $.post(url, { BenchSpace: BenchSpace }, function (data) {
                    //Update the data
                    $("table).html(data);
                  });
      });
谢谢


致以最诚挚的问候

我没有任何数据。我的会话已更新。我使用:$'tableid'.html;我想你不明白我想告诉你的意思。我的意思是,数据是服务器的响应,即response.WriteString;然后数据中就会有字符串。这就是数据在这里的作用,它与这里的数据或会话无关。。。