2017-04-20 60 views
0

我試圖從〜/文件夾/頁面顯示所選項目到根頁面名稱在asp.net 2015中產品,我已經在頁面中顯示了所有項目,但是因爲我稱之爲模型類有錯誤 「cannot implicitly convert type」 和一些錯誤,如無法從T1轉換爲T2

錯誤CS1503參數1:'不能從 'SAIT_Electronics.Pages.Product' 轉換爲「SAIT_Electronics.Product

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Web; 
 
using System.Web.UI; 
 
using System.Web.UI.WebControls; 
 
using SAIT_Electronics.Models; 
 
using System.Collections; 
 
using System.Management; 
 
using SAIT_Electronics.Pages; 
 
namespace SAIT_Electronics.Pages 
 
{ 
 
public partial class Product : System.Web.UI.Page 
 
    { 
 
     protected void Page_Load(object sender, EventArgs e) 
 
     { 
 
      Fillpage(); 
 
     } 
 

 
     private void Fillpage() 
 
     { 
 
      //get selected products data 
 
      if(!String.IsNullOrWhiteSpace(Request.QueryString["id"])) 
 
      { 
 
       int id = Convert.ToInt32(Request.QueryString["id"]); 
 
       productmodel model = new productmodel(); 
 
       Product product = model.GetProduct(id); 
 
       //fill page with data 
 
       lblPrice.Text = "Price per unit : <br/>$" + product.Price; 
 
           lblTitle.Text = product.Name; 
 
       lblDescription.Text = product.Description; 
 
       lblItemNr.Text = id.ToString(); 
 
       imgProduct.ImageUrl = "~/Images/Products/" + product.Image; 
 
       //fill amount dropdownlist with number 1 -10 
 
       int[] amount = Enumerable.Range(1, 10).ToArray(); 
 
       ddlAmount.DataSource = amount; 
 
       ddlAmount.AppendDataBoundItems = true; 
 
       ddlAmount.DataBind(); 
 
      } 
 
     } 
 
    } 
 
}

here is attached error picture of asp.net

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Web; 
 
using System.Web.UI; 
 
using System.Web.UI.WebControls; 
 
using System.Web.Configuration; 
 
using System.IO; 
 
using System.Collections; 
 
using SAIT_Electronics.Models; 
 

 

 

 

 
namespace SAIT_Electronics.Pages.Management 
 
{ 
 
    public partial class ManageProducts : System.Web.UI.Page 
 
    { 
 
     protected void Page_Load(object sender, EventArgs e) 
 
     { 
 
      if (!IsPostBack) 
 
      { 
 
       ImagesFolder(); 
 

 
       //check if the url contains an id parameter 
 
       if (!string.IsNullOrWhiteSpace(Request.QueryString["id"])) 
 
       { 
 
        int id = Convert.ToInt32(Request.QueryString["id"]); 
 
        FillPage(id); 
 
       } 
 
      } 
 
       
 
     } 
 

 

 
     protected void btnSubmit_Click(object sender, EventArgs e) 
 
     { 
 
      productmodel ProductModel = new productmodel(); 
 
      Product product = CreateProduct(); 
 

 
      //check if the url contains an id parameter 
 
      if (!string.IsNullOrWhiteSpace(Request.QueryString["id"])) 
 
      { 
 
       //id exist -> update existing row 
 
       int id = Convert.ToInt32(Request.QueryString["id"]); 
 
       lblResults.Text = ProductModel.UpdateProduct(id, product); 
 
      } 
 

 
      else 
 
      { 
 
       //id does not exist -> create a new row 
 
       lblResults.Text = ProductModel.InsertProduct(product); 
 
      } 
 
       
 
     } 
 

 
     private void FillPage(int id) 
 
     { 
 
      // get selected products from db 
 
      productmodel ProductModel = new productmodel(); 
 
      Product product = ProductModel.GetProduct(id); 
 

 
      //fill text boxes 
 
      txtDescription.Text = product.Description; 
 
      txtName.Text = product.Name; 
 
      txtPrice.Text = product.Price.ToString(); 
 

 
      //set dropdown list values 
 
      ddImage.SelectedValue = product.Image; 
 
      ddType.SelectedValue = product.TypeId.ToString(); 
 
     } 
 

 

 
     private void ImagesFolder() 
 
     { 
 
      try 
 
      { 
 
       //get all file paths 
 
       //string[] images = Directroy.GetFiles(Server.MapPath("~/Images/Products/")); 
 
       // string[] images = Server.MapPath(@"/Images/Products/"); 
 
       string[] images = System.IO.Directory.GetFiles(Server.MapPath("~/Images/Products/")); 
 
       // String images = new DirectoryInfo(@"D:\Desktop\Shoping\SAIT_Electronics\SAIT_Electronics\Images\Products\").Name; 
 

 
       //string[] images = Directory.GetFiles(("~/D:/Desktop/Shoping/SAIT_Electronics/SAIT_Electronics/Images/Products/"), "*.xml"); 
 
       //get all file names and add them to an arraylist 
 
       ArrayList imageList = new ArrayList(); 
 
       foreach (string image in images) 
 
       { 
 
        string imageName = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1); 
 
        imageList.Add(imageName); 
 
       } 
 

 
       // set the array list as dropdwon views datasouce and refresh 
 
       ddImage.DataSource = imageList; 
 
       ddImage.AppendDataBoundItems = true; 
 
       ddImage.DataBind(); 
 
      } 
 

 
      catch (Exception e) 
 
      { 
 
       lblResults.Text = e.ToString(); 
 
      } 
 
     } 
 

 

 
     private Product CreateProduct() 
 
     { 
 
      Product product = new Product(); 
 

 
      product.Name = txtName.Text; 
 
      product.Price = Decimal.Parse(txtPrice.Text); 
 
      product.TypeId = Convert.ToInt32(ddType.SelectedValue); 
 
      product.Description = txtDescription.Text; 
 
      product.Image = ddImage.SelectedValue; 
 

 
      return product; 
 
     } 
 

 
     
 
    } 
 
}

+0

變化到產品 –

+0

您方法'model.GetProduct(ID)返回類型model.GetProduct(ID)的;'必須返回一個不同的類型(可能_product_,但不是這個)。在這裏它是指向當前頁面Product',所以改變類型或者用完整的命名空間來指定它。 –

+0

是的錯誤由此刪除,但轉換錯誤保持不變。 – Asif

回答

0

Product類型在此行

Product product = model.GetProduct(id); 

Product類你在裏面,現在,這樣SAIT_Electronics.Pages.Product。試試這個:

SAIT_Electronics.Product product = model.GetProduct(id); 

或簡單:

var product = model.GetProduct(id); 

你應該做同樣在ManageProducts.aspx.cs文件。無論您在何處定義Product類型的變量,都可以用SAIT_Electronics.Product替換Product。例如線39變爲:

SAIT_Electronics.Product product = CreateProduct(); 

這同樣適用於線61,106和108

+0

感謝Vicari您的快速回復...通過使用這些更改,我可以刪除錯誤,但同樣的錯誤.......「無法從'SAIT_Electronics.Pages.Product'轉換爲'SAIT_Electronics.Product'」 – Asif

+0

你能告訴我哪一行給你錯誤嗎? – themiurge

+0

如果你可以看到我上傳的錯誤圖像.......可能你可以理解 – Asif

相關問題