1

Yes, I know there are dozens of problems like this but I think I have read them all and it still did not help me.

My project is based on well-known ScottGu's MVC tutorial. This is the error I get when I try to run the Nowy() action method (which means "new"):

The model item passed into the dictionary is of type 'SklepAlfa.Models.ProdutkyNowyViewModel', but this dictionary requires a model item of type 'SklepAlfa.Models.Produkty'.

This is my Nowy.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.Produkty>" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="SklepAlfa.Models" %>
<%@ Import Namespace="SklepAlfa.Views" %>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Nowy
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Dodaj produkt</h2>
<form action="/Produkty/Utworz" method="post"> <-- Also I don't know why in this line the path is underlined with a green wave-line and it says "File '/produkty/utworz' was not found', when it doesn't suppose to look for a file because "Utworz" ("Create") is an action method -->
<table>

    <tr>
        <td>Nazwa: </td>
        <td><input id="nazwa_produktu" type="text" name="nazwa_produktu" /></td>
    </tr>

    <tr>
        <td>Kategoria: </td>        
        <%= Html.DropDownListFor(model => model.kod_kategorii_produktu, new SelectList(ViewBag.Kategorie, "kod_kategorii_produktu", "nazwa_kategorii"))%>        
    </tr>

    <tr>
        <td>Cena w zł: </td>
        <td><input id="cena_produktu" type="text" name="cena_produktu" /></td>
    </tr>

    <tr>
        <td>Kolor: </td>
        <td><input id="kolor_produktu" type="text" name="kolor_produktu" /></td>
    </tr>

    <tr>
        <td>Wielkość: </td>
        <td><input id="wielkosc_produktu" type="text" name="wielkosc_produktu" /></td>
    </tr>

    <tr>
        <td>Opis: </td>
        <td><input id="opis_produktu" type="text" name="opis_produktu" /></td>
    </tr>

</table>

<input type="submit" value="Zapisz" />


</form>

</asp:Content>

I'm not sure if that "Import namespace"s are necessary.

Here's my Nowy.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SklepAlfa.Models;


namespace SklepAlfa.Views.Produkty
{
    public partial class Nowy : ViewPage<ProdutkyNowyViewModel>
    {
    }
}

ProduktyNowyViewModel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SklepAlfa.Models;

namespace SklepAlfa.Models
{
    public class ProdutkyNowyViewModel
    {
        public List<Kategorie_produktow> Kategorie { get; set; }
    }
}

This is the relevant code from my ProduktyController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SklepAlfa.Models;


namespace SklepAlfa.Models
{
    public class ProduktyController : Controller
    {

        SklepAlfaModelDataContext sklepBaza = new SklepAlfaModelDataContext();


    (...)


        public ActionResult Nowy()
        {
            ProdutkyNowyViewModel viewData = new ProdutkyNowyViewModel();
            viewData.Kategorie = sklepBaza.PobierzKategorieProduktow();


            return View("Nowy", viewData); // <- At this point application crashes throwing error, at least according to a breakpoints and my logic which is limited when it comes to programming ;)
        }



        public ActionResult Utworz(string nazwa_produktu, string kod_kategorii_produktu, int cena_produktu, string kolor_produktu, string wielkosc_produktu, string opis_produktu)
        {
            Produkty produkt = new Produkty();
            produkt.nazwa_produktu = nazwa_produktu;
            produkt.kod_kategorii_produktu = kod_kategorii_produktu;
            produkt.cena_produktu = cena_produktu;
            produkt.kolor_produktu = kolor_produktu;
            produkt.wielkosc_produktu = wielkosc_produktu;
            produkt.opis_produktu = opis_produktu;


            sklepBaza.DodajProdukt(produkt);
            sklepBaza.SubmitChanges();


            return RedirectToAction("Kategoria", "Produkty", new { id = produkt.kod_kategorii_produktu });



        }




    }
}

Relevant code from SklepAlfaModelDataContext.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SklepAlfa.Models;




namespace SklepAlfa.Models
{
    public partial class SklepAlfaModelDataContext
    {
        public List<Kategorie_produktow> PobierzKategorieProduktow()
        {

            return Kategorie_produktows.ToList();

        }

        public void DodajProdukt(Produkty produkt)
        {
            Produkties.InsertOnSubmit(produkt);
        }

       (...)

    }
}

Can someone help me please?

tereško
  • 58,060
  • 25
  • 98
  • 150
Lord_Blizzard
  • 25
  • 2
  • 9

2 Answers2

1

Change the first line :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.Produkty>" %>

to

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.ProdutkyNowyViewModel>" %>
neebz
  • 11,465
  • 7
  • 47
  • 64
  • I have tried it before but then in my Nowy.aspx in the following line: '<%= Html.DropDownListFor(model => model.kod_kategorii_produktu, new SelectList(ViewBag.Kategorie, "kod_kategorii_produktu", "nazwa_kategorii"))%>' it stops to recognize model properties and throws an error: SklepAlfa.Models.ProdutkyNowyViewModel does not contain a definition for kod_kategorii_produktu and no extension method kod_kategorii_produktu accepting a first argument of type SklepAlfa.Models.ProdutkyNowyViewModel could be found (are you missing a using directive or an assembly reference?) – Lord_Blizzard Jun 21 '11 at 11:17
  • that's a different issue. solve each issue one at a time – RPM1984 Jun 21 '11 at 11:32
  • You need to understand that what object are you binding your view with ? Now that we have changed it from `SkelpAlfa.Models.Produkty` to `SklepAlfa.Models.ProdutkyNowyViewModel`, your view can only access properties which are part of the `ProdutkyNowyViewModel`. It doesn't contain `kod_kategorii_produktu` property, that's why it doesn't find it and gives an error. Also search the internet to see how to use `DropDownListFor` too. – neebz Jun 21 '11 at 11:50
0

Isn't it obvious?

Action:

ProdutkyNowyViewModel viewData = new ProdutkyNowyViewModel();
return View("Nowy", viewData);

View:

<%@ ... Inherits="System.Web.Mvc.ViewPage<SklepAlfa.Models.Produkty>" %>

Change the Inherits to match the type of viewData, or vice versa.

RPM1984
  • 72,246
  • 58
  • 225
  • 350