gpt4 book ai didi

mysql - 数据表传输到模型,然后传输到 View

转载 作者:行者123 更新时间:2023-11-29 07:48:32 25 4
gpt4 key购买 nike

好吧,经过大量研究,我得出的结论是,将数据表传递给 View 是一个坏主意,那么我如何将数据表传递给模型,然后能够访问 View 中的每一行和每一列?抱歉,我是 MVC 新手

我从一个简单的 SQL 语句开始

StringBuilder sbSQL = new StringBuilder();
//// define a list of CustomerModel objects
DataSet tempDS = new DataSet();

//string xSQL = "SELECT PropertyAddress,PropertyTypeDesc,PropertyID FROM KDOR_vwPropertyGeneral ORDER BY PropertyAddress";
System.Data.SqlClient.SqlDataAdapter DbCmd = new System.Data.SqlClient.SqlDataAdapter();
string sqlWhereCont = " WHERE ";
sbSQL.Append("SELECT ");
sbSQL.Append("PropertyAddress As PropertyAddress,");
sbSQL.Append("PropertyTypeDesc As PropertyTypeDesc,");
sbSQL.Append("PropertyID as PropertyID");
sbSQL.Append(" FROM [KDOR_vwPropertyGeneral] ");
if (!string.IsNullOrEmpty(user.Address))
{
sbSQL.Append(sqlWhereCont + "(PropertyAddress) LIKE '" + user.Address + "%'");
sqlWhereCont = "AND ";
}
sbSQL.Append(" ORDER BY ");
sbSQL.Append(" PropertyAddress ");



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(sbSQL.ToString(), cnn);
cmd.CommandTimeout = 30000;
DbCmd.SelectCommand = cmd;

将数据移动到数据集和数据表

DbCmd.Fill(tempDS, "ResultSet");
DataTable resultSet = tempDS.Tables["ResultSet"];

将项目添加到模型

 var vm = new List<BedroomModel>();
foreach (DataRow dr in tempDS.Tables[0].Rows)
{
vm.Add(new BedroomModel {PropertyAdd = dr.ItemArray[0].ToString() });
vm.Add(new BedroomModel { PropertyDesc = dr.ItemArray[1].ToString() });
vm.Add(new BedroomModel { PropertyID = dr.ItemArray[2].ToString() });
}

现在如何访问每个项目并在 View 中循环遍历它们?因为我在这里收到错误,请查看我的 View

@model DataBaseTest.Models.BedroomModel

@{
ViewBag.Title = "Result";
}

<h2>Result</h2>
@{
ViewBag.Title = "Result";
}

<table border ="1">
<thead>
@* <tr>
@foreach (var col in Model.Columns) {
<th>
@col.ColumnName
</th>
}
</tr>*@
<tr>
<th>Property Address</th>
<th>Property Description</th>
<th>Property ID</th>
</tr>
</thead>

<tbody>

@foreach (var item in Model.PropertyAdd)
{
<tr>


<td>@Model.PropertyAdd</td>

</tr>
}
</tbody>

Model
namespace DataBaseTest.Models
{
public class BedroomModel
{
public string Address { get; set; }
public string PropertyAdd { get; set; }
public string PropertyID { get; set; }
public string PropertyDesc { get; set; }



public IEnumerable<BedroomModel> BedroomModels { get; set; }
}

}

再次抱歉,我是 MVC 新手任何建议将不胜感激。

最佳答案

您正在告诉您的 View ,它应该寻找单个 BedroomModel对象,当您实际上想向它传递 List<BedroomModel> 时目的。

@model List<DataBaseTest.Models.BedroomModel>

因此,您的 Model属性将是列表本身,所以你的 foreach循环只需要循环Model ,不是Model.BedroomModels .

@foreach (var item in Model)
{
<tr>
<td>@item.PropertyAdd</td>
<td>@item.PropertyDesc</td>
<td>@item.PropertyID</td>
</tr>
}

由于此更改,您可以删除 BedroomModels您的属性(property)BedroomModel类。

public class BedroomModel
{
public string Address { get; set; }
public string PropertyAdd { get; set; }
public string PropertyID { get; set; }
public string PropertyDesc { get; set; }
}

这可以解决你的问题,但我也注意到在填充你的 vm 时列表中,您要向列表中添加三项,而您应该只添加一项 BedroomModel目的。您的foreach循环应如下所示:

var vm = new List<BedroomModel>();
foreach (DataRow dr in tempDS.Tables[0].Rows)
{
vm.Add(new BedroomModel
{
PropertyAdd = dr.ItemArray[0].ToString(),
PropertyDesc = dr.ItemArray[1].ToString(),
PropertyID = dr.ItemArray[2].ToString()
};
}

您还必须确保将 ViewModel 发送到 View 中。您的操作应返回:

return View(vm);

关于mysql - 数据表传输到模型,然后传输到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com