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?