0

Every time when I want to check if Ilosc (Quantity) value is correct (I write "dsafga" in it to check if there is no way to write wrong value (not numeric)) I get this error:

The ViewData item that has the key 'Nazwa_przedmiotu' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

If all values are ok there is no error, but in time I write "dsasf" in numeric textbox this error appears. What is wrong in this code?

ItemShoppingLists Controller:

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);
}

Create View:

<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; }
}
Cezar
  • 345
  • 6
  • 18
  • `var x = cats.ToList()......; ViewBag.Przedmiot = x;` needs to be immediately before `return View(itemShoppingList);` There is no point populating it when you redirect (its needed only when you return the view). –  Aug 25 '16 at 22:52
  • OK that works with wrong value, but after wrong value if I type correct (float number) my ViewBag.ItemShoppingID = ID looses its value, what can I do with it? – Cezar Aug 26 '16 at 09:14
  • Remove `` from your view. You already have `@Html.HiddenFor(Model => Model.ItemShoppingID)` and remove the pointless `new { ItemShoppingID = ViewBag.ItemShoppingID }` - no idea what in the world you think that would do - learn to inspect the html your generating. –  Aug 26 '16 at 09:18
  • Then delete `ViewBag.ItemShoppingID = ID;` and initialize your model in the GET method and set the value of `ItemShoppingID` and return the model to the view - `var model = new ItemShoppingList(){ ItemShoppingID = ID }; return View(model);` –  Aug 26 '16 at 09:19
  • Do not use VIewBag (EVER) Set the property in your model and pass the model to the view (And it DOES solve the problem) –  Aug 26 '16 at 09:22
  • Thanks a lot, initializing model in GET method solved it. – Cezar Aug 26 '16 at 09:24

0 Answers0