0
votes

I'm very new at Mvc and Core so I have a problem with models in one View. I already have a project working right now as Webforms. So I builded a new project and finished biggest part. So that is my problem:

I attached the error. So there is my codes:

public class SiparisViewModel
{
    public siparis_detay SIPARISDETAYI { get; set; }
    public STOKCAMLAR STOKCAMLAR { get; set; }
}

This is my SiparislerController codes:

        [HttpGet]
    public IActionResult OlcuGirisi(int id)
    {
        ViewData["Title"] = "Sipariş Detayı";
        ViewData["sipno"] = id;

        string connectionString = configuration.GetConnectionString("DefaultConnection");


        //List<STOKCAMLAR> camlistesi = new List<STOKCAMLAR>();
        using (SqlConnection baglanti = new SqlConnection(connectionString))
        {
            string kayit = $"select * from STOKCAMLAR WHERE ID ='{id}'";
            SqlCommand komut = new SqlCommand(kayit, baglanti);
            komut.CommandType = CommandType.Text;
            baglanti.Open();
            SqlDataReader reader = komut.ExecuteReader();

            if (reader.Read())
            {
                STOKCAMLAR camlar = new STOKCAMLAR();
                camlar.ID = Convert.ToInt32(reader["ID"]);
                camlar.CAMSTOKKODU = reader["CAMSTOKKODU"].ToString();
                camlar.CAMACIKLAMA = reader["CAMACIKLAMA"].ToString();

                /// goes like this.

                return View(camlar);


            }
            baglanti.Close();

        }

        return View();
    }


[HttpPost]

public IActionResult OlcuGirisi(SiparisViewModel siparisviewmodel) {

        int id = 0;
        ViewData["sipno"] = id;

        string connectionString = configuration.GetConnectionString("DefaultConnection");

        using (SqlConnection baglanti = new SqlConnection(connectionString))
        {
            int siparisno = 901123;

            string kayit = "insert into siparis_detay(Siparis_no,stok_id,stok_kodu) values (@siparisno,@stokid,@stokkodu)";

            SqlCommand komut = new SqlCommand(kayit, baglanti);

            komut.Parameters.AddWithValue("@siparisno", "901124");
            komut.Parameters.AddWithValue("@stokkodu", "C0001");
            komut.Parameters.AddWithValue("@stokid", "0");

            /// it goes like this, long codes.

            //Parametrelerimize Form üzerinde ki kontrollerden girilen verileri aktarıyoruz.
            baglanti.Open();
            komut.ExecuteNonQuery();
            baglanti.Close();

            return View("OlcuGirisi", siparisviewmodel)

And this is my View:

@model KolaycamCore.ViewModel.SiparisViewModel

So when I open my page https://localhost:44364/Siparisler/OlcuGirisi/5 it gives me this error:

An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'KolaycamCore.Models.STOKCAMLAR', but this ViewDataDictionary instance requires a model item of type 'KolaycamCore.ViewModel.SiparisViewModel'. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)

So what am I doing wrong?

1
I believe you are trying to select a data value from your Database, type Model, and trying to put it into a ViewModel, type ViewModel. You will need to create a new ViewModel and match each value from your model over to the ViewModel. - Kevin
So, just copy codes from both model to inside ViewModel? - Tuğhan Avcı
Generally mine goes like this. ViewModel vm = new { m.Id, m.Date, m.Data }; - Kevin
Ok, I didn't do that but made a new ViewModel and put all model codes inside of it. Problem solved. Thank you. - Tuğhan Avcı

1 Answers

0
votes

You are passing wrong model to view you have to pass SiparisViewModel instead of STOKCAMLAR

Because your view expect SiparisViewModel , so your code to return view is below,

 if (reader.Read())
        {
            SiparisViewModel siparis = new SiparisViewModel();
            siparis.ID = Convert.ToInt32(reader["ID"]);
            siparis.CAMSTOKKODU = reader["CAMSTOKKODU"].ToString();
            siparis.CAMACIKLAMA = reader["CAMACIKLAMA"].ToString();

            /// goes like this.

            return View(siparis);


        }