具有键&#39; Nazwa_przedmiotu&#39;的ViewData项目。属于&#39; System.String&#39;但必须属于&#39; IEnumerable <selectlistitem>&#39;

时间:2016-08-25 15:34:03

标签: asp.net-mvc key viewdata

每当我想检查Ilosc(数量)值是否正确时(我写&#34; dsafga&#34;在其中检查是否无法写错值(不是数字))我得到这个错误:

  

具有键&#39; Nazwa_przedmiotu&#39;的ViewData项目。属于&#39; System.String&#39;但必须是&#39; IEnumerable&lt; SelectListItem&gt;&#39;

如果所有值都没问题就没有错误,但是我及时写了#34; dsasf&#34;在数字文本框中出现此错误。这段代码有什么问题?

ItemShoppingLists控制器:

public ActionResult Create(int ID)
{
    var cats = from b in db.Items
               select new { b.Nazwa };

    var x = cats.ToList().Select(c => new SelectListItem
    {
        Text = c.Nazwa,
        Value = c.Nazwa
    }).ToList();

    ViewBag.Przedmiot = x;
    ViewBag.ItemShoppingID = ID;
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ISListID,Nazwa_przedmiotu,Ilosc,ItemShoppingID")] ItemShoppingList itemShoppingList)
{
    if (itemShoppingList.Ilosc > 0)
    {
        var duplicate = (from c in db.ItemShoppingLists
                         where c.Nazwa_przedmiotu == itemShoppingList.Nazwa_przedmiotu && c.ItemShoppingID == itemShoppingList.ItemShoppingID
                         select c).FirstOrDefault();
        if(duplicate != null)
        {     
            return RedirectToAction("Details", "ItemShoppings", new { id = itemShoppingList.ItemShoppingID, error = "Już masz ten przedmiot na liście. Edytuj jego ilość zamiast dodawać go drugi raz!" });
        }
        else
        {
            var cats = from b in db.Items
                       select new { b.Nazwa };

            var x = cats.ToList().Select(c => new SelectListItem
            {
                Text = c.Nazwa,
                Value = c.Nazwa
            }).ToList();

            ViewBag.Przedmiot = x;
            db.ItemShoppingLists.Add(itemShoppingList);
            db.SaveChanges();
            return RedirectToAction("Details", "ItemShoppings", new { id = itemShoppingList.ItemShoppingID });
        }   
    }
    return View(itemShoppingList);
}

创建视图:

<div class="form-horizontal">
    <h4>@ViewBag.ErrorMessage</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Nazwa_przedmiotu, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Nazwa_przedmiotu, ViewBag.Przedmiot as IEnumerable<SelectListItem>, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Nazwa_przedmiotu, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Ilosc, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Ilosc, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Ilosc, "", new { @class = "text-danger" })
        </div>
    </div>
    <input type="hidden" name="ItemShoppingID" value="@ViewBag.ItemShoppingID" />
    @Html.HiddenFor(Model => Model.ItemShoppingID, new { ItemShoppingID = ViewBag.ItemShoppingID })


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

ItemShoppingList Model:

public class ItemShoppingList
{
    [Key]
    public int ISListID { get; set; }

    [Display(Name = "Nazwa przedmiotu")]
    public string Nazwa_przedmiotu { get; set; }

    [Display(Name = "Ilość")]
    public float Ilosc { get; set; }

    public int ItemShoppingID { get; set; }
    public virtual ItemShopping ItemShopping { get; set; }
}

0 个答案:

没有答案