运行此代码时,出现此错误:
“ /”应用程序中的服务器错误。
传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [DAL.DBEntities.tblStudent]',但此字典需要模型项为'DAL.Model.ClassPromotion'。
这是我的ClassPromotionController
:
public class ClassPromotionController : BaseController
{
studentRepository stdRepo;
staffRepository parRepo;
profileRepository profileRepo;
accessControlRepository accessRepo;
staffcategoryRepository stcRepo;
SecurityRepository secRepo;
classRepository classRepo;
schoolRepository schoolRepo;
public ClassPromotionController()
{
if (currentUser != null)
{
schoolRepo = new schoolRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
stdRepo = new studentRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
parRepo = new staffRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
profileRepo = new profileRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
accessRepo = new accessControlRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
classRepo = new classRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
stcRepo = new staffcategoryRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
secRepo = new SecurityRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
}
}
public ActionResult Index(int? ddlClassID)
{
if (currentUser != null)
{
schoolRepo = new schoolRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
stdRepo = new studentRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
parRepo = new staffRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
profileRepo = new profileRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
accessRepo = new accessControlRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
classRepo = new classRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
stcRepo = new staffcategoryRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
secRepo = new SecurityRepository(new DBEntities(base.BuildConnectionString(currentUser.SqlDataSource, currentUser.SqlDatabase, currentUser.SqlUser, currentUser.SqlPassword)));
}
}
public ActionResult Index(int? ddlClassID)
{
var info = schoolRepo.getSchoolInfo();
ViewBag.SessionList = schoolRepo.getSessionList();
ViewBag.ClassList = classRepo.getClassList();
ViewBag.ClassID = ddlClassID;
return View(stdRepo.getStudentList(currentUser.User.SchoolInfo.Session, ddlClassID));
}
}
这是视图(index.html
):
@model DAL.Model.ClassPromotion
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
DAL.Model.UserSession sess = (DAL.Model.UserSession)HttpContext.Current.Session["UserSession"];
}
<hr />
<h4>
<i class="fa fa-arrow-circle-right"></i>
<b>Manage Class Promotion</b>
</h4>
<hr />
<div class="form-group">
<div class="row">
<div class="col-md-12">
<label class="col-md-1 control-label">From Class</label>
<div class="col-md-3">
@Html.DropDownList("ddlClassID", new SelectList(ViewBag.ClassList, "ClassID", "ClassName", (object)ViewBag.ClassID), "All", new Dictionary<string, object> { { "class", "form-control selectpicker" }, { "data-live-search", "true" } })
@Html.ValidationMessage("ddlClassID")
</div>
<label class="col-md-3 control-label">Session :</label>
<div class="col-md-9">
@Html.DropDownListFor(model => model.Session, new SelectList(ViewBag.SessionList, "Session", "Session"), new Dictionary<string, object> { { "class", "form-control selectpicker" }, { "data-live-search", "true" } })
</div>
<label class="col-md-1 control-label">To Class</label>
<div class="col-md-3">
@Html.DropDownList("ddlClassID", new SelectList(ViewBag.ClassList, "ClassID", "ClassName", (object)ViewBag.ClassID), "All", new Dictionary<string, object> { { "class", "form-control selectpicker" }, { "data-live-search", "true" } })
@Html.ValidationMessage("ddlClassID")
</div>
<label class="col-md-3 control-label">Session :</label>
<div class="col-md-9">
@Html.DropDownListFor(model => model.Session, new SelectList(ViewBag.SessionList, "Session", "Session"), new Dictionary<string, object> { { "class", "form-control selectpicker" }, { "data-live-search", "true" } })
</div>
<span class="pull-right">
<button id="AddNew" class="btn btn-danger ">Promoted</button>
</span>
</div>
</div>
</div>
答案 0 :(得分:0)
我没有足够的声誉点可以发表评论,因此我很抱歉将其发布为解决方案。
但是,查看模型会很有帮助。我不一定认为您的控制器是问题所在。我注意到的一件事是“ @model DAL.Model.ClassPromotion”'Model.ClassPromotion'全部为蓝色。通常,该文件夹不是蓝色的。但是我不确定您的文件夹结构是什么样的,或者该名称是否合法。
那么您的文件夹名称是什么,传递给View的模型是什么?
此外,您可以将“新的DBEntities(base.BuildConnectionString(currentUser.SqlDataSource,currentUser.SqlDatabase,currentUser.SqlUser,currentUser.SqlPassword)”或至少其中一部分转换为变量,以便于阅读。肯定有更好的方法,尤其是在使用Entity Framework的情况下,但这并不是问题所在。