如何使用简单的Ajax Beginform在Asp.net MVC 4? [英] How to use Simple Ajax Beginform in Asp.net MVC 4?

查看:136
本文介绍了如何使用简单的Ajax Beginform在Asp.net MVC 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Asp.net MVC和我研究了约 Ajax.BeginForm 但是当我申请codeS没有奏效。你可以用视图,控制器,型号分享很简单的例子与 Ajax.Beginform ? 谢谢你。

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.

查看:

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

同时也可作为TSmith说在注释中,鸵鸟政策忘记安装的 jQuery的不显眼的Ajax 的通过的的NuGet

这篇关于如何使用简单的Ajax Beginform在Asp.net MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆