在这里,我需要从视图中的模型列表中获取最后一个值而不循环它..这是我的控制器代码
public IList<ProductDetailModel> GetWPDetails()
{
ProductDetailModel Viewmodel;
string funname = "GetCSpecialWPDetails";
List<ProductDetailModel> getWPDetails = new List<ProductDetailModel>();
getWPDetails = objrest.EcommerceWPDetails(funname);
List<ProductDetailModel> WPDetails = new List<ProductDetailModel>();
foreach (var item in getWPDetails)
{
Viewmodel = new ProductDetailModel();
Viewmodel.Productid = item.Productid;
Viewmodel.ProductName = item.ProductName;
Viewmodel.CategoryID = item.CategoryID;
Viewmodel.ProductRate = item.ProductRate;
Viewmodel.DiscountRate = item.DiscountRate;
Viewmodel.imageurl1 = item.imageurl1;
WPDetails.Add(Viewmodel);
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT ThemeColor,LayoutDesign FROM dbo.Cpecial_Partner_Design WHERE [PartnerID]='" + Partid + "'", con);
con.Open();
using (SqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
Viewmodel = new ProductDetailModel();
Viewmodel.ThemeColor = myReader["ThemeColor"].ToString();
Viewmodel.LayoutDesign = myReader["LayoutDesign"].ToString();
WPDetails.Add(Viewmodel);
}
myReader.Close();
}
con.Close();
return WPDetails;
}
在这里,我通过循环模型来获取值,总计数为 47,但我需要的是我只需要第 47 个值,即最后一个值,没有任何循环。
查看代码
@foreach (var item in Model)
{
@Html.EditorFor(modelItem => item.ThemeColor)
@Html.EditorFor(modelItem => item.LayoutDesign)
}
有什么建议吗?