2016-01-23 18 views
1

我是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財產上更新忽略這兩個字段,然後更新都會好起來的。我只是想了解爲什麼我不能在數據庫已經建立的時候從數據庫訪問這些列。任何幫助,將不勝感激

+0

你能還顯示視圖的代碼? – jvanrhyn

+0

@ jvanrhyn將編輯暫停 – Tartar

+0

嘗試將字段添加到視圖作爲隱藏字段。 '@ H​​tml.HiddenFor(x => x.UserCreatedId);' – jvanrhyn

回答

3

正如@jvanrhyn在您的問題評論中提到的那樣。如果你想要UserCreatedIdUserUpdatedId不是空值,那些字段必須在你的表單中。

如果您不想顯示它們,那麼您應該使用@Html.HiddenFor()輔助函數,或者在舊HTML上使用plain。

問題是,一旦你提交表格,只有你的<form>裏面的東西得到提交。您的表單沒有這些字段,因此當您提交時,UserCreatedIdUserUpdatedId默認設置爲空。

編輯:

@Tartar你有幾個選項。

  1. 一個是在您的窗體中使用Html.HiddenFor()。數據 將不會顯示在您的視圖中,但它仍然位於您的 表單中,因此當您提交時,您將在 UserCreatedId和UserUpdatedId中獲得值。這個解決方案是很好的,當你需要一個快速和簡單的修復程序時 。
  2. 另一種解決方案將是接着實施IRepository圖案 http://www.codeproject.com/Articles/526874/Repository-pattern-done-right

    代替db.Entry(公司).STATE = EntityState。改性;你 可以使用一個通用的方法,通過一個單一的 主鍵(例如companyId)能得到貴公司的數據,如:

    public TEntity Get(string id) where TEntity : class 
        { 
         return _session.Get<TEntity>(id); 
        } 
    

    一旦你必須從你的數據庫中的數據,你只更新 你想要的值。實施IRepository模式可能會有點過頭,但這是一個很好的模式,也是很好的練習。

  3. 我能想到的最後一個選項是顯示這些值,但是讓 在您的視圖中禁用它們(不允許編輯它們)。這是最容易修復的 ,但這是一個懶惰的修復。

整體而言,這取決於你想要達到的,如果你想澄清什麼是您的應用程序的目的(學習項目,學校項目,工作),我將能夠給你一個更詳細的解決方案是什麼:)

編輯: 仰視你的代碼後,我想出了一個快速和良好的(我猜)解決方案:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit(Company company) 
    { 

    try{ 
     if (ModelState.IsValid) 
     { 
      var companyToUpdate = db.Companies.Find(company.companyId).FirstOrDefault(); 
      /../ 
      /some update logic or what you need to do. companyToUpdate has ALL the values you need./ 
      /../ 

      db.Update(companyToUpdate); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(company); 
}catch(Exception ex){ 
//do something if exception occurs. 
} 

    } 
+0

@Tartar這是正確的。 – Aizen

+0

有道理。我應該怎麼做,通過發送一個LINQ查詢到數據庫來控制這些字段,就像我在Edit Post操作中已經做過的那樣?它也不起作用。 – Tartar

-2

而不是直接使用您的Model類,創建一個ViewModel類,只有你想要在您的視圖的字段,然後對此進行驗證。在控制器中,您可以將字段映射到模型(用戶),並添加任何計算值或額外的外鍵映射。

不要使用隱藏的字段作爲你想要計算的東西,因爲客戶端可以操縱它們。您可以使用ModelState.Remove("[Field name you do not want validated]");來停止驗證任何字段。

+0

你必須明白這一點。問題是爲什麼。不是如何。他希望得到澄清,而不是解決方案。 – Aizen

+0

@Aizen夠公平的。瞭解使用隱藏字段的含義以及您應該(或不)給予它們的信任級別很重要,因爲它們可以由惡意客戶僞造。 – DeveloperUK

相關問題