首先发布在这里,所以如果这篇文章不是很好,我很抱歉! 我目前正在使用asp.net构建一个Web应用程序,并遇到了这个问题:
传递到字典中的模型项的类型为“Daily_Planner.Models.DayPlanPartialview”,但此字典需要“Daily_Planner.Models.Group”类型的模型项
似乎我在传递正确的模型时遇到了问题。我正在使用几个部分视图,我担心我可能会使用我的模型错误,但我无法找出它是什么。我尝试过提供足够的信息,但是如果你需要更多信息,我会更愿意发布更多的代码。
部分视图_GroupManage:
@model IEnumerable<Daily_Planner.Models.Group>
<select>
@foreach (var item in Model)
{
<option value="@item.Id">@item.GroupName</option>
}
</select>
部分视图_layout:
@if (Request.IsAuthenticated)
{
@Html.ActionLink("Daily Planner", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
}
else
{
@Html.ActionLink("Daily Planner", "Login", "Account", new { area = "" }, new { @class = "navbar-brand" })
}
@if (Request.IsAuthenticated)
{
{ Html.RenderPartial("_GroupManage"); }
@Html.Partial("_LoginPartial")
}
型号:
namespace Daily_Planner.Models
{
public class DayPlanPartialview
{
public DayPlan displayComments { get; set; }
public Group groupM { get; set; }
public IEnumerable<Daily_Planner.Models.DayPlan> ListofComments { get; set;}
public IEnumerable<Daily_Planner.Models.Group> groupmanage{ get; set; }
}
的HomeController
namespace Daily_Planner.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
private DayPlanPartialview pv = new DayPlanPartialview();
[Authorize]
public ActionResult Index()
{
// Add list of comments with newest comment first // .Take(10).ToList(); will only show the last 10 posts
pv.ListofComments = db.DayPlans.OrderByDescending(x => x.DateCreated).ToList();
return View(pv);
}
[HttpPost]
[Authorize]
public ActionResult Index(DayPlanPartialview dayP)
{
dayP.displayComments.ApplicationUserId = User.Identity.GetUserId();
dayP.displayComments.DateCreated = DateTime.Now;
db.DayPlans.Add(dayP.displayComments);
db.SaveChanges();
pv.ListofComments = db.DayPlans.ToList();
return RedirectToAction("");
}
@model IEnumerable<Daily_Planner.Models.Group>
<select>
@foreach (var item in Model)
{
<option value="@item.Id">@item.GroupName</option>
}
</select>