3

ここでは、ループせずにビュー内のモデル リストから最後の値を取得する必要があります。これが私のコントローラー コードです。

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)
       } 

なにか提案を?

4

2 に答える 2

8

リンクを使おう!あなたの場合. Last()メソッド。

試す:

@{ var last = Model.Last(); }
@Html.EditorFor(modelItem => last.ThemeColor)
@Html.EditorFor(modelItem => last.LayoutDesign)
于 2013-01-28T10:56:11.840 に答える
1

あなたのモデルは IList<> のように見えるので、単純にこれを使用することをお勧めします:

var last;
if (Model != null && Model.Count > 0) last = Model[Model.Count - 1];
else
...
于 2013-01-28T10:54:45.810 に答える