本文介绍了如何在 Asp.net MVC 4 中使用 Simple Ajax Beginform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Asp.net MVC 的新手,我研究了 Ajax.BeginForm
但是当我应用代码时它不起作用.你能用 Ajax.Beginform
和视图、控制器、模型分享非常简单的例子吗?谢谢.
解决方案
简单示例:带有文本框和搜索按钮的表单.
如果您在textbox
中输入姓名"并提交表格,它会在表格中为您带来带有姓名"的患者.
查看:
@using (Ajax.BeginForm("GetPatients", "Patient", new AjaxOptions {//GetPatients 是 PatientController 中的方法名InsertionMode = InsertionMode.Replace,//目标元素(#patientList)将被替换UpdateTargetId = "患者名单",LoadingElementId = "loader"//带有 .gif 加载器的 div - 在加载数据时显示})){字符串患者姓名 = "";@Html.EditorFor(x=>patient_Name)//带有名称和ID的文本框,它将传递给控制器<input type="submit" value="搜索"/>}@* ... *@<div id="loader" class="alert" style="display:none">正在加载...<img src="~/Images/ajax-loader.gif"/>
@Html.Partial("_patientList") @* 这是带有患者表的视图.您将从控制器返回的相同视图 *@
_patientList.cshtml:
@model IEnumerable<table id="patientList" ><tr><th>@Html.DisplayNameFor(model =>model.Name)</th><th>@Html.DisplayNameFor(model =>model.Number)</th></tr>@foreach(模型中的var患者){<tr><td>@Html.DisplayFor(modelItem =>patient.Name)</td><td>@Html.DisplayFor(modelItem =>patient.Number)</td></tr>}
Patient.cs
公共类患者{公共字符串名称 { 获取;放;}public int Number{ 得到;放;}}
PatientController.cs
public PartialViewResult GetPatients(stringpatient_Name=""){var患者 = yourDBcontext.Patients.Where(x=>x.Name.Contains(patient_Name))返回 PartialView("_patientList", 患者);}
而且正如 TSmith 在评论中所说,不要忘记通过 .
I am new in Asp.net MVC and i researched about Ajax.BeginForm
but when i apply codes it did not work. Can you share very simple example with Ajax.Beginform
with View, Controller, Model?Thanks.
解决方案
Simple example: Form with textbox and Search button.
If you write "name" into the textbox
and submit form, it will brings you patients with "name" in table.
View:
@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" class=" aletr" style="display:none">
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);
}
And also as TSmith said in comments, don´t forget to install jQuery Unobtrusive Ajax library through NuGet.
这篇关于如何在 Asp.net MVC 4 中使用 Simple Ajax Beginform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!