2021-01-08

Ajax辅助方法通过“@Ajax”调用,如“@Ajax.ActionLink”,"@Ajax.BeginForm"

使用Ajax时,需要引入库文件

如Script文件夹中不存在jquery.unobtrusive-ajax.js时,可在NuGet程序包中安装:
Ajax.ActionLink方法
可以创建一个具有异步行为的超链接
ActionLink方法的第一个参数是超链接的文本,第二个参数是操作方法的名称
ActionLink方法可以通过设置AjaxOptions对象的属性值来调整Ajax请求的行为
AjaxOptions对象能够配置对服务器的异步请求以及处理返回的数据
程序:

HomeController.cs

public ActionResult Index()
{
return View(“index”);
}

    public ActionResult Getdate()
    {
        return Content(DateTime.Now.ToString());
    }

Index.cshtml

<script type="text/javascript">
    function msg(data) {
        alert(data);
    }
</script>
@*点击按钮从服务器获取时间,显示在id为show标签上*@ @Ajax.ActionLink("获取时间方法", "getdate", "home", new AjaxOptions { Confirm="确认要提交吗?", HttpMethod="post", InsertionMode=InsertionMode.Replace, OnSuccess="msg", UpdateTargetId="show" }); 获取到的时间:

Ajax.BeginForm方法
AjaxOptions对象和Ajax.ActionLink方法相同

程序:
HomeController.cs

    TestEntities db = new TestEntities();

    public ActionResult Index()
    {
        List<JobSeeker> list = db.JobSeeker.ToList();
        return View("index", list);
    }
  
    public ActionResult select(string UserName)
    {
        List<JobSeeker> list = db.JobSeeker.Where(u => u.Name.Contains(UserName)).ToList();
        return PartialView("result", list);
    }

Index.cshtml

@model List<MvcTest.Models.JobSeeker>



</head>
<body>
    @using(Ajax.BeginForm("select", "home", null, new AjaxOptions() {
            HttpMethod = "post",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId="show",
    }))
    {
        <table class="table table-bordered table-hover">
            <tr>
                <td>查询:</td>
                <td><input type="text" name="UserName" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="查询" /></td>
            </tr>
        </table>
    }

    <div id="show">
        <table class="table table-bordered table-hover">
              <thead>
                  <tr>
                      <td>姓名</td>
                      <td>求职意向</td>
                      <td>工作经验</td>
                      <td>教育程度</td>
                  </tr>
              </thead>
            <tbody>
                @foreach(var item in Model)
                {
                     <tr>
                         <td>@item.Name</td>
                         <td>@item.Career</td>
                         <td>@item.Experience</td>
                         <td>@item.Education</td>
                     </tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>

result.cshtml

@model List<MvcTest.Models.JobSeeker>

@foreach (var item in Model) { }
姓名求职意向工作经验教育程度
@item.Name@item.Career@item.Experience@item.Education
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值