Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我在更新过程中得到了一个意外的变量值_C#_Asp.net_Asp.net Mvc_Entity Framework_Model View Controller - Fatal编程技术网

C# 我在更新过程中得到了一个意外的变量值

C# 我在更新过程中得到了一个意外的变量值,c#,asp.net,asp.net-mvc,entity-framework,model-view-controller,C#,Asp.net,Asp.net Mvc,Entity Framework,Model View Controller,我正在使用与它们的文件名对应的以下代码。我正在使用实体框架,我得到一个变量urun作为null,因此我无法更新它 乌拉圭控制员- public ActionResult UrunGetir(int id) { var product = db.Urunler.Find(id); IEnumerable<SelectListItem> basetypes = db.Kategoriler.Select(

我正在使用与它们的文件名对应的以下代码。我正在使用实体框架,我得到一个变量
urun
作为
null
,因此我无法更新它

乌拉圭控制员-

 public ActionResult UrunGetir(int id)
        {
            var product = db.Urunler.Find(id);

            IEnumerable<SelectListItem> basetypes = db.Kategoriler.Select(
            b => new SelectListItem { Value = b.kategoriID.ToString(), Text = b.kategoriAd });
            ViewData["basetype"] = basetypes;

            return View("UrunGetir", product);
        }

public ActionResult Guncelle(Urunler p)
    {
        var urun = db.Urunler.Find(p.urunID);
        urun.urunAd = p.urunAd;
        urun.urunMarka = p.urunMarka;
        urun.urunStok = p.urunStok;
        urun.urunFiyat = p.urunFiyat;

        //pr.urunKategori = product.urunKategori;
        var category = db.Kategoriler.Where(m => m.kategoriID == p.Kategoriler.kategoriID).FirstOrDefault();
        urun.urunKategori = category.kategoriID;

        db.SaveChanges();
        return RedirectToAction("Index");
    }
public ActionResult-UrunGetir(int-id)
{
var product=db.Urunler.Find(id);
IEnumerable basetypes=db.Kategoriler.Select(
b=>new SelectListItem{Value=b.kategoriID.ToString(),Text=b.kategoriAd});
ViewData[“basetype”]=基本类型;
返回视图(“UrunGetir”,产品);
}
公众行动结果Guncelle(Urunler p)
{
var urun=db.Urunler.Find(p.urunID);
urun.urunAd=p.urunAd;
urun.urunMarka=p.urunMarka;
urun.urunStok=p.urunStok;
urun.urunFiyat=p.urunFiyat;
//pr.urunKategori=product.urunKategori;
var category=db.Kategoriler.Where(m=>m.kategoriID==p.Kategoriler.kategoriID).FirstOrDefault();
urun.urunKategori=category.kategoriID;
db.SaveChanges();
返回操作(“索引”);
}
UrunGetir.cshtml-

@model StokTakip.Models.Entity.Urunler
@{
    ViewBag.Title = "UrunGetir";
    Layout = "~/Views/Shared/_MainLayout.cshtml";
}

<style>
    .form-content {
        padding: 5%;
        border: 1px solid #ced4da;
        margin-bottom: 2%;
    }

    .form-control {
        border-radius: 1.5rem;
    }

    .btnSubmit {
        border: none;
        border-radius: 1.5rem;
        padding: 1%;
        width: 20%;
        cursor: pointer;
        background: #0062cc;
        color: #fff;
    }
</style>

@using (Html.BeginForm("Guncelle", "Urun", FormMethod.Post))
{
    <div class="register-form">
        <div class="form">

            <div class="form-content">
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>Ürün Adı :</label>
                            @*<input type="text" class="form-control" name="urunAd" />*@
                            @Html.TextBoxFor(m=> m.urunAd, new { @class="form-control"})
                        </div>
                        <div class="form-group">
                            <label>Ürün Marka :</label>
                            @*<input type="text" class="form-control" name="urunMarka" />*@
                            @Html.TextBoxFor(m=> m.urunMarka, new {@class="form-control" })
                        </div>

                        <div class="form-group">
                            <label>Ürün Kategori :</label>

                            @*@Html.TextBoxFor(m=> m.urunKategori,new { @class="form-control"})*@
                            @Html.DropDownListFor(m => m.Kategoriler.kategoriID, ViewBag.basetype as IEnumerable<SelectListItem>, new { @class = "form-control" })
                        </div>
                        <div class="form-group">
                            <label>Ürün Fiyat :</label>
                            @*<input type="text" class="form-control" name="urunFiyat" />*@

                            @Html.TextBoxFor(m=> m.urunFiyat,new {@class="form-control" })
                        </div>
                        <div class="form-group">
                            <label>Ürün Stok :</label>
                            @*<input type="text" class="form-control" name="urunStok" />*@
                            @Html.TextBoxFor(m => m.urunStok, new { @class = "form-control" })

                        </div>

                    </div>

                </div>

                <button class="btnSubmit"> Güncelle</button>
                <a href="/Urun/Index/" class="btnSubmit" style="background-color:red;">Geri Dön</a>
            </div>
        </div>
    </div>
}
@model StokTakip.Models.Entity.Urunler
@{
ViewBag.Title=“UrunGetir”;
Layout=“~/Views/Shared/\u MainLayout.cshtml”;
}
.表格内容{
填充:5%;
边框:1px实心#ced4da;
利润底部:2%;
}
.表格管制{
边界半径:1.5雷姆;
}
.btnSubmit{
边界:无;
边界半径:1.5雷姆;
填充:1%;
宽度:20%;
光标:指针;
背景#0062cc;
颜色:#fff;
}
@使用(Html.BeginForm(“Guncelle”、“Urun”、FormMethod.Post))
{
Ürün Adı
@**@
@TextBoxFor(m=>m.urunAd,新的{@class=“form control”})
Ürün Marka:
@**@
@TextBoxFor(m=>m.urunMarka,new{@class=“form control”})
Ürün Kategori:
@*@TextBoxFor(m=>m.urunKategori,new{@class=“form control”})*@
@DropDownListFor(m=>m.Kategoriler.kategoriID,ViewBag.basetype为IEnumerable,新的{@class=“form control”})
Ürün Fiyat:
@**@
@TextBoxFor(m=>m.urunFiyat,新的{@class=“form control”})
Ürün Stok:
@**@
@TextBoxFor(m=>m.urunStok,新的{@class=“form control”})
甘塞尔
}
Index.cshtml-

@using StokTakip.Models.Entity
@model List<Urunler>


@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_MainLayout.cshtml";
}

<a style="margin-bottom:20px;" href="/Urun/UrunEkle/" class="btn btn-primary">Yeni Ürün Ekle</a>
<table class="table table-striped">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Ürün Adı</th>
            <th scope="col"> Marka</th>
            <th scope="col"> Kategori</th>
            <th scope="col"> Fiyat</th>
            <th scope="col"> Stok</th>
            <th scope="col"> Sil</th>
            <th scope="col"> Güncelle</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var product in Model)
        {
            <tr>
                <th scope="row">@product.urunID</th>
                <td>@product.urunAd</td>
                <td>@product.urunMarka</td>
                <td>@product.Kategoriler.kategoriAd</td>
                <td>@product.urunFiyat <strong>₺</strong> </td>
                <td>@product.urunStok</td>
                <td><a href="/Urun//Sil/@product.urunID" class="btn btn-danger">Sil</a></td>
                <td><a href="/Urun//UrunGetir/@product.urunID" class="btn btn-success">Güncelle</a></td>

            </tr>
        }



    </tbody>
</table>
@使用StokTakip.Models.Entity
@模型列表
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/\u MainLayout.cshtml”;
}
#
Ürün Adı
马尔卡
卡泰戈里
菲亚特
斯托克
Sil
甘塞尔
@foreach(模型中的var乘积)
{
@产品名称:乌鲁尼
@产品名称:urunAd
@product.urunMarka
@product.Kategoriler.kategoriAd
@product.urunFiyat
@产品名称:urunStok
}
var-urun=db.Urunler.Find(p.urunID)

产品变量为
null
,因此我无法更新它。 数据库中没有问题。我正在使用实体框架。

请添加

@Html.HiddenFor(t=> t.urunID)

在UrunGetir.cshtml

中,假设您从实体框架中得到错误。那么就没有必要在你的问题中添加观点了。另外,请添加您获得的异常,以便更新您需要将id发送到后端,如果未设置id,请重新指定默认值(0),并且id为0,请查找您的实体以更新它谢谢。有些页面没有这样做。这就是我问的原因。当你不发送id时,你不能使用它。