我現在正在創建MVC 5.0 C#中的web應用程序,我的作品停止了,因爲我有一點問題。我有Packs
類,接口Packs
- IPacksrepository
和PacksController
。與use.something不同
Packs.cs
namespace Domain.Entities
{
public class Packs
{
public int PacksID { get; set; }
public string Name { get; set; }
public string OwnerDescripiton { get; set; }
public decimal wymiarX { get; set; }
public decimal wymiarY { get; set; }
public decimal wymiarZ { get; set; }
public decimal Waga { get; set; }
public string dataprzyjeciadomagazynu { get; set; }
public string datawyslaniazmagazynu { get; set; }
public string dataodebrania { get; set; }
public int WarehousesID { get; set; }
[ForeignKey("WarehousesID")]
public Warehouses Warehouses { get; set; }
}
}
IPackRepostiory
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Entities;
namespace Domain.Abstract
{
public interface IPacksRepository
{
IEnumerable<Packs> Packss { get; }
void SavePacks(Packs packs);
Packs DeletePacks(int PacksID);
}
}
PackController
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entities;
using Domain.Abstract;
using WebUi.Models;
namespace WebUi.Controllers
{
public class PacksController : Controller
{
private IPacksRepository repository;
public int PageSize = 1;
public PacksController(IPacksRepository packrepostiory)
{
this.repository = packrepostiory;
}
public ViewResult List()
{
return View(repository.Packss);
}
public ViewResult Index()
{
return View(repository.Packss);
}
public ViewResult Create()
{
return View("Edit", new Packs());
}
public ActionResult Details(int? PacksID)
{
Packs packs = repository.Packss.FirstOrDefault(p => p.PacksID == PacksID);
return View(packs);
}
public ViewResult Edit(int? PacksID)
{
Packs packss = repository.Packss.FirstOrDefault(p => p.PacksID == PacksID);
return View(packss);
}
[HttpPost]
public ActionResult Edit(Packs packs)
{
if (ModelState.IsValid)
{
repository.SavePacks(packs);
TempData["message"] = string.Format("Zapisano {0} ", packs.Name);
return RedirectToAction("Index");
}
else
{
return View(packs);
}
}
public ActionResult Delete(int PacksID)
{
Packs delpacks = repository.DeletePacks(PacksID);
if (delpacks != null)
{
TempData["message"] = string.Format("Usunięto {0}", delpacks.Name);
}
return RedirectToAction("Index");
}
}
}
在我的項目,我有幾個類,interfaces
和Controllers
(如persons
,warehouses
, transportfleet
) - 所有功能在結構上相似,只有數據集不同。
所有我「名字」存儲庫使用using Domain.Entities
和一切都OK,所有工作。 Domain.Entities
包含Pack.cs
等。 IPacksRepostiory
即使它包含未使用的名稱using Domain.Entities
。通過我在PackController
得到錯誤狀PackController
:
public ViewResult Edit(int? PacksID)
{
Packs packss = repository.Packss.FirstOrDefault(p => p.PacksID == PacksID);
return View(packss);
}
repository.Packss.FirstOrDefault(p值=> p.PacksID == PacksID); - > 錯誤CS1503參數1:不能從轉換 'Domain.Entities.Packs' 爲 'Domain.Packs'
它不工作,告訴我的請爲什麼別人我「東西」 repository.cs使用Domain.Entities(HIG是高亮),當我在Packs.cs中實現「使用Domain.Entities」時,是不是使用? (未突出顯示) –
@MikeEvans var'關鍵字應該有效,除非我誤解了你的問題! –
在某些情況下,工作,但我想知道爲什麼在IPackRepository,當我嘗試使用「使用Domain.Entities」我不能? (未突出顯示)。在其他情況下(如IPersonRepository「我可以使用 –