[请查看我的以下与文件名相对应的代码:-我正在使用实体框架,并且将变量urun
设置为null
,因此无法对其进行更新。请帮助。
UrunController-
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");
}
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>
}
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>
var urun = db.Urunler.Find(p.urunID);
产品变量为null
,因此我无法对其进行更新。数据库中没有问题。我正在使用实体框架。谁能帮我解决问题?
请添加
@Html.HiddenFor(t=> t.urunID)
在UrunGetir.cshtml中