我試圖從〜/文件夾/頁面顯示所選項目到根頁面名稱在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;
}
}
}
變化到產品 –
您方法'model.GetProduct(ID)返回類型model.GetProduct(ID)的;'必須返回一個不同的類型(可能_product_,但不是這個)。在這裏它是指向當前頁面Product',所以改變類型或者用完整的命名空間來指定它。 –
是的錯誤由此刪除,但轉換錯誤保持不變。 – Asif