我是ASP.NET MVC 5的新手,我正在開發一個簡單的應用程序。ASP.NET MVC 5:如果沒有顯示在視圖中,則無法從控制器訪問列
我在我的模型中有兩個字段(UserCreatedId。UserUpdatedId),我想在控制器中創建/更新它們,但它們不顯示在視圖中。
當我嘗試從編輯操作訪問這些字段時,它們顯示爲空,這會導致外鍵衝突。
查看:
@model AuditTrackingApp.Models.Company
@{
ViewBag.Title = "Şirket Bilgilerini Düzenle";
}
@section navigationMenu{
<nav>
@{ Html.RenderAction("GetNavMenuItems", "Home"); }
</nav>
}
<h2>Şirket Bilgilerini Düzenle</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CompanyId)
<div class="form-group">
@Html.LabelFor(model => model.CompanyName, "Şirket Adı", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyAddress, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.CompanyAddress1, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CompanyAddress1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyCountry, "Ülke", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyCountry, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyCountry, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyCity, "İl", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyCity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyCity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyCounty, "İlçe", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyCounty, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyCounty, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyZipCode, "Posta Kodu", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyZipCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyZipCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreationTime, "Oluşturulma Zamanı", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreationTime, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.CreationTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UpdateTime, "Güncellenme Zamanı", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UpdateTime, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.UpdateTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="DEĞİŞİKLİKLERİ KAYDET" class="btn btn-primary" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Şirket Listesine Dön", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Company company = db.Companies.Find(id);
if (company == null)
{
return HttpNotFound();
}
return View(company);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CompanyId,CompanyName,CompanyAddress,CompanyCountry,CompanyCity,CompanyCounty,CompanyZipCode,UserCreatedId,CreationTime,UserUpdatedId,UpdateTime")] Company company)
{
if (ModelState.IsValid)
{
db.Entry(company).State = EntityState.Modified;
//HardCoded for UserCreatedId and UserUpdatedId now. UserId will be taken from session later
User user = (from i in db.Users where i.UserId == 1 select i).SingleOrDefault();
int companyToUpdateId = company.CompanyId;
Company companyToUpdate = (from i in db.Companies where i.CompanyId == companyToUpdateId select i).SingleOrDefault();
company.UserCreatedId = companyToUpdate.UserCreatedId;
company.UserUpdatedId = user.UserId;
company.UpdateTime = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(company);
}
我知道我可以通過IsModified
財產上更新忽略這兩個字段,然後更新都會好起來的。我只是想了解爲什麼我不能在數據庫已經建立的時候從數據庫訪問這些列。任何幫助,將不勝感激
你能還顯示視圖的代碼? – jvanrhyn
@ jvanrhyn將編輯暫停 – Tartar
嘗試將字段添加到視圖作爲隱藏字段。 '@ Html.HiddenFor(x => x.UserCreatedId);' – jvanrhyn