2017-02-25 58 views
0

在我的其中一個程序中,我設置了一個「編輯」視圖/操作方法,它可以讓我編輯特定表項的信息。編輯器選項有效,但不會使用當前信息填充編輯器。這導致「編輯」選項看起來與我的「創建」選項相同,這是爲了讓用戶創建一個新條目。爲什麼我的編輯器沒有預先填充?

有什麼我失蹤了嗎?該視圖的代碼似乎與我正在關注的教程幾乎完全相同,但它們顯示的結果是您對「編輯」選項的期望;一位編輯預先填寫了條目的當前信息。

以下是我的視圖和操作方法的代碼。提前致謝。

查看:

@model PharmTrack.Models.Doctor 

@{ 
ViewBag.Title = "Edit"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Edit</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Doctor</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.firstName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.lastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.address, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.address, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.address, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.NPI, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.NPI, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.NPI, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.DEA, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.DEA, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.DEA, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Save" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
@Html.ActionLink("Back to Index", "Index") 
</div> 

操作方法:

public ActionResult Edit(int id) 
    { 
     return View(); 
    } 
+0

這個問題是最有可能在控制器中。請向我們展示Controller的操作。 – NineBerry

+0

@NineBerry我已經添加了它。我認爲它是微不足道的,因爲它只是返回視圖。當然,如果方法中缺少某些東西,那是有道理的。 – AustinC

回答

1

你需要得到你想要編輯到模型類的一個實例,並提供該到視圖對象的數據。

這樣的代碼:

public ActionResult Edit(int id) 
{ 
    PharmTrack.Models.Doctor doctor = LoadDoctorFromDatabase(id); 
    return View(doctor); 
} 
+0

我實際上只是寫了類似的東西,它的工作。但我更喜歡你的解決方案;謝謝您的幫助! – AustinC

+0

@AustinC這是默認的Asp.net MVC方式。不建議從視圖內或類似的東西中加載模型的數據。 – NineBerry

相關問題