传递到字典中的模型项的类型为List`1,但此字典需要IEnumerable类型的模型项

时间:2016-05-04 19:05:31

标签: c# asp.net-mvc entity-framework

提前感谢您的帮助。

MVC的新手可能很容易回答。我收到这个错误,不知道它有什么意思。这是我的代码:

我的观点:

@model carBayWebsite.Models.WishList

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm("Create",
  "WishLists",
  FormMethod.Post,
  new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Add to WishList?</h4>
    <hr />
    <p>Are you sure you want to add this to your Wish List?</p>
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.HiddenFor(m => m.AdvertId)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.HiddenFor(m => m.AdvertId)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Add" class="btn btn-default"/>
        </div>
    </div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我的控制器

public class WishListsController : Controller
{
    private Entities2 db = new Entities2();

    public ActionResult Index()
    {
        string userName = User.Identity.GetUserName();
        var wishlist = from ads in db.Adverts
                      join wish in db.WishLists
                      on ads.AdvertId equals wish.AdvertId
                      where wish.UserId.Contains(userName)
                      select wish;

        return View(wishlist.ToList());
    }

    // GET: WishLists/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        WishList wishList = db.WishLists.Find(id);
        if (wishList == null)
        {
            return HttpNotFound();
        }
        return View(wishList);
    }

    public ActionResult Create(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        else
        { 
        string usID = User.Identity.GetUserId().ToString();
        int adID = Convert.ToInt32(id);
        WishList wishList = db.WishLists.Find(id);
        ViewBag.userId = usID;
        ViewBag.advertId = adID;
            return View(wishList);
        }




    }

    // POST: WishLists/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(WishList wishList)
    {
        if (ModelState.IsValid)
        {

            db.WishLists.Add(new WishList
            {
                UserId = ViewBag.userId,
                AdvertId = Convert.ToInt32(ViewBag.advertID),
            });
                        try {
                        db.SaveChanges();
                        }

                        catch (DbEntityValidationException dbEx)
                            {
                                foreach (var validationErrors in dbEx.EntityValidationErrors)
                                {
                                    foreach (var validationError in validationErrors.ValidationErrors)
                                    {
                                        Trace.TraceInformation("Property: {0} Error: {1}", 
                                                                validationError.PropertyName, 
                                                                validationError.ErrorMessage);
                                    }
                                }
                            }


            return RedirectToAction("Index");
        }
        return View(wishList);


    }

有谁能弄清楚出了什么问题?从环顾四周来看,我觉得它的东西非常简单,就像第一个看起来像View一样,但是我不能把它固定下来! :)

错误详情

  

传递到字典中的模型项的类型为'System.Collections.Generic.List1 [carBayWebsite.Models.Advert]',但此字典需要类型为'System.Collections.Generic.IEnumerable1 [carBayWebsite]的模型项。 Models.WishList]'

1 个答案:

答案 0 :(得分:0)

我向你展示的观点是Create我认为。(因为标题是'创建')

您要转到Create查看whishList,它是WhisList的类型return View(wishList);

但您的观点在视图开头等待Advert@model carBayWebsite.Models.Advert

我认为这是问题所在。如果不是,请告诉我。