生成ELEMENT UI表格与ASP.NET MVC 动态交互数据与模糊查询的代码
时间: 2024-03-17 17:40:55 浏览: 116
以下是一个基于ASP.NET MVC和Element UI的动态交互数据与模糊查询的示例代码:
HTML代码:
```html
<template>
<div>
<el-input v-model="searchText" placeholder="请输入搜索关键字"></el-input>
<el-button type="primary" @click="searchData">搜索</el-button>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="id" label="ID"></el-table-column>
<el-table-column prop="name" label="名称"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</div>
</template>
```
JavaScript代码:
```javascript
<script>
export default {
data() {
return {
tableData: [],
searchText: ''
}
},
methods: {
fetchData() {
axios.get('/api/data', {
params: {
searchText: this.searchText
}
}).then(response => {
this.tableData = response.data
})
},
searchData() {
this.fetchData()
}
},
mounted() {
this.fetchData()
}
}
</script>
```
ASP.NET MVC代码:
```csharp
public class DataController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetData(string searchText)
{
var data = new List<object> {
new { id = 1, name = "张三", age = 20, address = "北京市" },
new { id = 2, name = "李四", age = 25, address = "上海市" },
new { id = 3, name = "王五", age = 30, address = "广州市" },
new { id = 4, name = "赵六", age = 35, address = "深圳市" }
};
if (!string.IsNullOrEmpty(searchText))
{
data = data.Where(x => x.name.Contains(searchText)).ToList();
}
return Json(data, JsonRequestBehavior.AllowGet);
}
}
```
在上面的示例中,我们使用Element UI中的el-input和el-table组件来实现动态交互数据与模糊查询。在JavaScript代码中,我们使用axios库发送AJAX请求到ASP.NET MVC的控制器中获取数据,并将数据绑定到el-table组件中进行展示。在ASP.NET MVC的控制器中,我们使用JsonResult返回JSON格式的数据,并根据searchText参数进行模糊查询。
阅读全文