我正在尝试执行转移到View的原始SQL语句。我需要有一个模型来保存所有信息,还是只需传递DataTable?这是我的代码。我只是需要帮助将数据提供给视图才能看到它。
namespace DataBaseTest.Controllers
{
public class HomeController : Controller
{
DataTable dtFindResults = new DataTable();
public ActionResult Index()
{
//// define a list of CustomerModel objects
DataSet tempDS = new DataSet();
string xSQL = "SELECT PropertyAddress FROM KDOR_vwPropertyGeneral ORDER BY PropertyAddress";
System.Data.SqlClient.SqlDataAdapter DbCmd = new System.Data.SqlClient.SqlDataAdapter();
//// populate a list of CustomerModel objects from database
string MyConnectionString = ConfigurationManager.ConnectionStrings["WLConnection"].ConnectionString;
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(MyConnectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(xSQL, cnn);
cmd.CommandTimeout = 30000;
DbCmd.SelectCommand = cmd;
DbCmd.Fill(tempDS,"ResultSet");
dtFindResults = tempDS.Tables["ResultSet"];
var x = dtFindResults.Rows.Count;
cnn.Close();
//// return the list of CustomerModel objects to our View
return View(dtFindResults);
}
答案 0 :(得分:0)
您需要一个ViewModel。
public class CustomerModel
{
public string PropertyAddress { get; set; }
}
var vm = new List<CustomerModel>();
foreach (var item in dtFindResults)
{
vm.Add(new CustomerModel { PropertyAddress = /* value from DataSet */ });
}
return View(vm);
最后,您的DataSet应该来自一个单独的问题,比如Repository。此存储库应返回可以发送到View的ViewModel列表。