栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Asp.net MVC 4中使用Simple Ajax Beginform?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Asp.net MVC 4中使用Simple Ajax Beginform?[关闭]

简单示例:带有文本框和“搜索”按钮的表单。

如果您在“

textbox
并提交”表格中填写“姓名” ,则会为您带来表中带有“姓名”的患者。

视图:

@using (Ajax.BeginForm("GetPatients", "Patient", new AjaxOptions {//GetPatients is name of method in PatientController    InsertionMode = InsertionMode.Replace, //target element(#patientList) will be replaced    UpdateTargetId = "patientList",    LoadingElementId = "loader" // div with .gif loader - that is shown when data are loading   })){    string patient_Name = "";    @Html.EditorFor(x=>patient_Name) //text box with name and id, that it will pass to controller    <input  type="submit" value="Search" />}@* ... *@<div id="loader"  >    Loading...<img src="~/Images/ajax-loader.gif" /></div>@Html.Partial("_patientList") @* this is view with patient table. Same view you will return from controller *@

_ PatientList.cshtml:

@model IEnumerable<YourApp.Models.Patient><table id="patientList" ><tr>    <th>        @Html.DisplayNameFor(model => model.Name)    </th>    <th>        @Html.DisplayNameFor(model => model.Number)    </th>       </tr>@foreach (var patient in Model) {<tr>    <td>        @Html.DisplayFor(modelItem => patient.Name)    </td>    <td>        @Html.DisplayFor(modelItem => patient.Number)    </td></tr>}</table>

Patient.cs

public class Patient{   public string Name { get; set; }   public int Number{ get; set; }}

PatientController.cs

public PartialViewResult GetPatients(string patient_Name=""){   var patients = yourDBcontext.Patients.Where(x=>x.Name.Contains(patient_Name))   return PartialView("_patientList", patients);}

而且,正如TSmith在评论中所说,不要忘记通过NuGet安装
jQuery Unobtrusive Ajax
库。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号