如何根据MVC中的列表框选择显示表 [英] How to display a table based on listbox selection in MVC
本文介绍了如何根据MVC中的列表框选择显示表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
包含列名的列表框(列名基于另一个列表框动态更改)。现在如何在mvc4中显示列表框列的表格格式。
< b>我尝试了什么:
我在asp.net c#中试过但我需要在mvc中,这里下拉选择项是表名。这个我在mvc中需要相同的代码。
List box containing column names(column name changing dynamically based on another list box).Now how to display table format for list box columns in mvc4.
What I have tried:
I tried in asp.net c# but i need in mvc,here drop down select item is table name.This same code i need in mvc.
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
List<string> listbfrom = new List<string>();
for (int i = 0; i < ListBox1.Items.Count; i++)
if (ListBox1.Items[i].Selected)
listbfrom.Add(ListBox1.Items[i].Text);
// string csv = string.Join(",", listbfrom);
//string csv = string.Format("'{0}'", string.Join("','", listbfrom));
string csv = string.Join(",", listbfrom.ToArray());
string csv1 = DropDownList1.SelectedItem.Text;
con.Open();
string str = "SELECT "+csv+ " FROM "+csv1;
using (SqlCommand com = new SqlCommand(str, con))
{
// com.Parameters.AddWithValue("@listbfrom", csv);
// com.Parameters.AddWithValue("@tabfrom", DropDownList1.SelectedItem.Text);
DataSet dsBooking = new DataSet();
SqlDataAdapter dap = new SqlDataAdapter(com);
dap.Fill(dsBooking);
con.Close();
GridView1.DataSource = dsBooking;
GridView1.DataBind();
}
/* protected void Button2_Click(object sender, EventArgs e)
{
//ListBox1_SelectedIndexChanged(sender, e);
if (ListBox1.Items.Count > 0)
{
DataTable dt = new DataTable();
DataSet dsBooking = new DataSet();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
string Listbfrom = ListBox1.Items[i].Text;
string str = "SELECT " + Listbfrom + " FROM " + DropDownList1.SelectedItem.Text;
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter dap = new SqlDataAdapter(com);
dap.Fill(dsBooking);
}
}
GridView1.DataSource = dsBooking;
GridView1.DataBind();
}
}*/
/* protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
string Listbfrom = ListBox1.Items[i].Text;
con.Open();
string str = "SELECT " + Listbfrom + " FROM " + DropDownList1.SelectedItem.Text;
SqlCommand com = new SqlCommand(str, con);
DataSet dsBooking = new DataSet();
SqlDataAdapter dap = new SqlDataAdapter(com);
dap.Fill(dsBooking);
con.Close();
{
GridView1.DataSource = dsBooking;
GridView1.DataBind();
}
}
}
}
}
*/
}
}
推荐答案
控制器:大>
Controller:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace mvc.Controllers
{
public class HomeController : Controller
{
string constr = "your conneciton string";
public ActionResult Index()
{
return View();
}
public ActionResult GetAllTableNames() {
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select name from sys.tables " , con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
var data = dt.AsEnumerable().Select(k => k["name"].ToString());
return Json(data);
}
public ActionResult GetAllColumnNames(string tbl)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tbl " , con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@tbl", tbl);
DataTable dt = new DataTable();
da.Fill(dt);
var data = dt.AsEnumerable().Select(k => k["COLUMN_NAME"].ToString());
return Json(data);
}
public ActionResult GetTableData(string tableName, string columnNames)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select " + columnNames + " from " + tableName, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
string data = JsonConvert.SerializeObject(dt);
return Json(data);
}
}
}
CSHTML
CSHTML
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
(function(){
(function () {
.ajax({
url:'@ Url.Action (GetAllTableNames,home)',
数据:{},
类型:'post',
contentType:application / json; charset = utf-8,
dataType:json,
成功:函数(数据){
var ddl =
.ajax({ url: '@Url.Action("GetAllTableNames", "home")', data: {}, type: 'post', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { var ddl =
这篇关于如何根据MVC中的列表框选择显示表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文