Share via

I need to pass db tables items to another db tables

Mehmet Gül 1 Reputation point
2022-01-20T07:16:09.523+00:00

Hi everbody;

I have two temporary db tables and two main tables, I want to insert items of temporary tables to main tables, I have model, controller and view these are in the below

this is Model
public IEnumerable<sepetUrun> sepetUruns { get; set; }
public IEnumerable<sepetMusteri> sepetMusteris { get; set; }

these are temporary tables
table 1 - sepetUrun
public partial class sepetUrun
{
public int Id { get; set; }
public string UrunAdi { get; set; }
public Nullable<int> UrunAdet { get; set; }
public Nullable<decimal> UrunTutar { get; set; }
public string UrunKod { get; set; }
}

table 2 - sepetMusteri
public partial class sepetMusteri
{
public int Id { get; set; }
public string MusteriAdi { get; set; }
public string MusteriKod { get; set; }
}

these are main tables
table 1 - Fatura
public partial class Fatura
{
public int Id { get; set; }
public string FaturaUrunAdi { get; set; }
public Nullable<int> FaturaMiktar { get; set; }
public Nullable<decimal> FaturaTutar { get; set; }
public string FaturaUrunKod { get; set; }
public string FaturaNo { get; set; }
}
table 2 - FaturaMus
public partial class FaturaMus
{
public int Id { get; set; }
public string MusteriAdi { get; set; }
public string MusteriKod { get; set; }
public string FaturaNo { get; set; }
}

controller is
public ActionResult Onay2(Fatura p, FaturaMus dp, string MusteriAdi, string MusteriKod, string UrunAdi, int UrunAdet, decimal UrunTutar, string UrunKod)
{
p.FaturaUrunAdi = UrunAdi;
p.FaturaUrunKod = UrunKod;
p.FaturaMiktar = UrunAdet;
p.FaturaTutar = UrunTutar;
db.Fatura.Add(p);

     dp.MusteriAdi = MusteriAdi;  
     dp.MusteriKod = MusteriKod;  
     db.FaturaMus.Add(dp);  
     db.SaveChanges();  
     return RedirectToAction("FaturaHazirla2");  
 }  

my view is that
@默 WebApplication1.Models.UrunMusteriVM

@{
ViewBag.Title = "FaturaHazirla2";
Layout = "~/Views/Shared/_Layout2.cshtml";
}

<h2>FaturaHazirla2</h2>

@using (Html.BeginForm("Onay2", "Satis", FormMethod.Post))
{
<table>
<tr>
<th>Müşteri Adı</th>
<th>Müşteri Kodu</th>
</tr>
@for (var i = 0; i < Model.sepetMusteris.Count(); i++)
{
<tr>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetMusteris[i].MusteriAdi, new { @readonly = "readonly", @DeezNutz = "MusteriAdi" })</td>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetMusteris[i].MusteriKod, new { @readonly = "readonly", @DeezNutz = "MusteriKod" })</td>
</tr>
}
</table>
<table>
<tr>
<th>Ürün Adı</th>
<th>Ürün Miktarı</th>
<th>Ürün Tutarı</th>
<th>Ürün Kodu</th>
</tr>
@for (var i = 0; i < Model.sepetUruns.Count(); i++)
{
<tr>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetUruns[i].UrunAdi, new { @readonly = "readonly", @DeezNutz = "UrunAdi" })</td>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetUruns[i].UrunAdet, new { @readonly = "readonly", @DeezNutz = "UrunAdet" })</td>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetUruns[i].UrunTutar, new { @readonly = "readonly", @DeezNutz = "UrunTutar" })</td>
<td>@azzedinehtmlsql .TextBoxFor(x => x.sepetUruns[i].UrunKod, new { @readonly = "readonly", @DeezNutz = "UrunKod" })</td>
</tr>
}
</table>

<div><button class="btn btn-primary">Onayla</button></div>
}

but when I run this project I got this error

The parameters dictionary contains a null entry for parameter 'UrunAdet' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Onay2(WebApplication1.Models.Fatura, WebApplication1.Models.FaturaMus, System.String, System.String, System.String, Int32, System.Decimal, System.String)' in 'WebApplication1.Controllers.SatisController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

how can I handle it?

Developer technologies | ASP.NET Core | Other
0 comments No comments

2 answers

Sort by: Most helpful
  1. AgaveJoe 31,361 Reputation points
    2022-01-20T18:28:08.06+00:00

    I gave the value however it could not accepted, I do that

    How are you verifying UrunAdet is submitted and contains a value? Are you using the browser's dev tools?

    Anyway, this error is easily reproduced using the following code sample and the URL; https://localhost:44344/home/Onay2.

    public ActionResult Onay2(int UrunAdet)  
    {  
        return View();  
    }  
    

    The following error is thrown because the parameter is not passed to the action.

    The parameters dictionary contains a null entry for parameter 'UrunAdet' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Onay2(Int32)' in 'MvcDemo.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
    Parameter name: parameters

    Following the instructions in the error, change the UrunAdet input parameter to a nullable type "int?" or Nullable<int>.

    public ActionResult Onay2(int? UrunAdet)  
    {  
        return View();  
    }  
    

    Now the Onay2 actions input parameter matches the model definition.

    public Nullable<int> UrunAdet { get; set; }  
    

    Typically, you want to use a model in the action parameters rather than a list of values that are a copy of the model properties. I recommend the following tutorials for learning standard MVC 5 programming patterns.

    Getting Started with ASP.NET MVC 5
    Getting Started with Entity Framework 6 Code First using MVC 5

    Was this answer helpful?


  2. AgaveJoe 31,361 Reputation points
    2022-01-20T14:29:16.843+00:00

    The action method defines UrunAdet as an int which cannot be null. The caller, which you did not share, must supply a route value.

    Your tags is for MVC Core but I think your code is ASP.NET MVC not core. So the following link explains how routes work in ASP.NET MVC.

    ASP.NET MVC Routing Overview (C#)

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.