【问题标题】:insert item one table to another table mvc entity将项目一个表插入到另一个表 mvc 实体
【发布时间】:2022-01-19 15:23:16
【问题描述】:

我想将项目从一个数据库表复制到另一个表,我有模型,两个数据库表,但是当我运行视图时出现错误,在下面我的模型、表和控制器和视图中,

我的模型是

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

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

sepetMusteri 数据库是

        public int Id { get; set; }
        public string MusteriAdi { get; set; }
        public string MusteriKod { get; set; }

我的控制器是

            public ActionResult FaturaHazirla2()
        {
            UrunMusteriVM urunMusteriVM = new UrunMusteriVM();

            urunMusteriVM.sepetUruns = db.sepetUrun.ToList();

            urunMusteriVM.sepetMusteris = db.sepetMusteri.ToList();

            return View(urunMusteriVM);
        }

        public ActionResult Onay2(FaturaMus p, Fatura dp, string MusteriAdi, string MusteriKod, string UrunAdi, int UrunAdet, decimal UrunTutar, string UrunKod)
        {
            p.MusteriAdi = MusteriAdi;
            p.MusteriKod = MusteriKod;

            dp.FaturaUrunAdi = UrunAdi;
            dp.FaturaMiktar = UrunAdet;
            dp.FaturaTutar = UrunTutar;
            dp.FaturaUrunKod = UrunKod;

            db.FaturaMus.Add(p);
            db.Fatura.Add(dp);
            db.SaveChanges();
            return View("FaturaHazirla2");
        }

我的看法是


    <form action="/Satis/Onay2/">
    <table>
        <tr>
            <th>Müşteri Adı</th>
            <th>Müşteri Kodu</th>
        </tr>
        @foreach (var item in Model.sepetMusteris)
        {
            <tr>
                <td>@Html.TextBoxFor(x => item.MusteriAdi, new { @readonly = "readonly", name = "MusteriAdi2" })</td>
                <td>@Html.TextBoxFor(x => item.MusteriKod, new { @readonly = "readonly", name = "MusteriKod2" })</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>
        @foreach (var item in Model.sepetUruns)
        {
            <tr>
                <td>@Html.TextBoxFor(x => item.UrunAdi, new { @readonly = "readonly" })</td>
                <td>@Html.TextBoxFor(x => item.UrunAdet, new { @readonly = "readonly" })</td>
                <td>@Html.TextBoxFor(x => item.UrunTutar, new { @readonly = "readonly" })</td>
                <td>@Html.TextBoxFor(x => item.UrunKod, new { @readonly = "readonly" })</td>
            </tr>
        }
    </table>

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

但是当我运行它时,我得到了这个错误

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.FaturaMus, WebApplication1.Models.Fatura, 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.

我该如何解决这个问题

【问题讨论】:

    标签: asp.net model-view-controller entity


    【解决方案1】:

    修复模型

    public List<sepetUrun> sepetUruns { get; set; }
    public List<sepetMusteri> sepetMusteris { get; set; }
    

    将模型添加到视图中,由于要提交数据,因此必须使用 for 循环而不是 foreach

    @model UrunMusteriVM
    
    @using (Html.BeginForm("UrunMusteriVM", "Satis", FormMethod.Post)) 
    { 
      .....
    
       @for (var i=0; i < Model.sepetMusteris.Count; i++)
            {
                <tr>
                    <td>@Html.TextBoxFor(x => x.sepetMusteris[i].MusteriAdi)</td>
                    <td>@Html.TextBoxFor(x => x.sepetMusteris[i].MusteriKod)</td>
                </tr>
            }
      
    
            ... the same for another table....
       
     <div><button type="submit" class="btn btn-primary">Onayla</button></div>
    
    } 
    

    并修复操作

     public ActionResult Onay2(UrunMusteriVM model)
    {
    ....
    }
    

    【讨论】:

    • 亲爱的塞尔吉;我无法修复 actionresult 部分,请您给我更多详细信息,
    • @MehmetGÜL 我帮不了你,因为我不知道 FaturaMus p、Fatura dp 是什么,你把它们带到了哪里
    • FaturaMus 是“公共部分类 FaturaMus { public int Id { get; set; } public string MusteriAdi { get; set; } public string MusteriKod { get; set; } public string FaturaNo { get; set ; } }” 并且 Fatura 是“public partial class Fatura { public int Id { get; set; } public string FaturaUrunAdi { get; set; } public Nullable FaturaMiktar { get; set; } public Nullable FaturaTutar {获取;设置;} 公共字符串 FaturaUrunKod { 获取;设置;} 公共字符串 FaturaNo { 获取;设置;} }"
    • @MehmetGÜL 它与您的视图模型有什么关系?
    • 我需要将 sepetUrun 和 sepetMusteri db 表项插入 Fatura 和 FaturaMus db 表,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多