2016-12-22 5 views
0

私はしばらくの間、ビューモデルに問題がありました。私のビューモデルでは、私は "インデックス"を表示することができ、私は新しい従業員 "作成"を追加することができますが、 "編集"は機能しません。MVC viewmodel Httppostの後に編集が何も表示されない

「編集」ページを表示したり、名前を変更するなどの編集を行いますが、投稿時にすべてのデータがnullと表示されます。 "作成"では、挿入後、コントローラは変更(EmployeeViewModel)を表示してレコードを挿入します。それは単に "編集"を行う時を示していません。

これはビューモデルに固有のものか、それとも何か他にありますか?ここで

は私のviewmodelクラス(最初のデータベース)です:

public partial class Employee 
    { 
     public int EmployeeId { get; set; } 
     public string Name { get; set; } 
     public Nullable<int> DepartmentId { get; set; } 
     public string Address { get; set; } 

     public virtual Department Department { get; set; } 
    } 

public partial class Department 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public Department() 
     { 
      this.Employees = new HashSet<Employee>(); 
     } 

     public int DepartmentId { get; set; } 
     public string DepartmentName { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Employee> Employees { get; set; } 
    } 

    public class EmployeeViewModel 
    { 
     public int EmployeeId { get; set; } 
     public string Name { get; set; } 
     public Nullable<int> DepartmentId { get; set; } 
     public string Address { get; set; } 
     public string DepartmentName { get; set; } 

    } 

ここに私のコントローラです:

public class TestController : Controller 
    { 
     public db dContext = new db(); 
     public ActionResult Index() 
     { 
      List<Employee> employeelist = dContext.Employees.ToList(); 
      EmployeeViewModel employeeVM = new EmployeeViewModel(); 

      List<EmployeeViewModel> employeeVMList = employeelist.Select(x => new EmployeeViewModel 
      { 
       Name = x.Name, 
       EmployeeId = x.EmployeeId, 
       Address = x.Address, 
       DepartmentId = x.DepartmentId, 
       DepartmentName = x.Department.DepartmentName 
      }).ToList(); 
      return View(employeeVMList);  
     } 
     public ActionResult Create() 
     { 
      return View(); 
     } 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create(EmployeeViewModel employeeVM) 
     { 
      if (ModelState.IsValid) 
      { 
       Employee e = new Employee(); 
       e.EmployeeId = employeeVM.EmployeeId; 
       e.Name = employeeVM.Name; 
       e.DepartmentId = employeeVM.DepartmentId; 
       e.Address = employeeVM.Address; 

       dContext.Employees.Add(e); 
       dContext.SaveChanges(); 

       return RedirectToAction("Index"); 
      } 
      return View("Index"); 
     } 

     public ActionResult Edit(EmployeeViewModel em , int? id) 
     { 
      var dbEmpVM = (from e in dContext.Employees 
          join d in dContext.Departments 
          on e.DepartmentId equals d.DepartmentId 
          where e.EmployeeId == id 
          select new EmployeeViewModel 
          { 
           EmployeeId = e.EmployeeId, 
           DepartmentId=e.DepartmentId, 
           Address=e.Address, 
           Name=e.Name, 
           DepartmentName=d.DepartmentName 
          }).ToList();  
      return View(dbEmpVM ); 
     } 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit(EmployeeViewModel model, int id) 
     { 
      string name = Request.Form["EmployeeId"]; 
      string naaanm = model.EmployeeId.ToString(); 

      return RedirectToAction("Index"); 
     } 
    } 

そして、ここには私の編集です:

@model IEnumerable<MVCTutorial.Models.EmployeeViewModel> 
@{ 
    ViewBag.Title = "Edit"; 
} 
<h4>(EmployeeViewModel)</h4> 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    foreach (var item in Model) 
    { 
<div class="form-group"> 
    @Html.LabelFor(i => item.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(i => item.EmployeeId, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(i => item.EmployeeId, "", new { @class = "text-danger" }) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(i => item.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(i => item.Name, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(i => item.Name, "", new { @class = "text-danger" }) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(i => item.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(i => item.DepartmentId, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(i => item.DepartmentId, "", new { @class = "text-danger" }) 
    </div> 
</div> 
     <div class="form-group"> 
      @Html.LabelFor(i => item.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(i => item.DepartmentName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(i => item.DepartmentName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(i => item.Address, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(i => item.Address, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(i => item.Address, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    } 
<div class="form-group"> 
    <div class="col-md-offset-2 col-md-10"> 
     <input type="submit" value="Edit" class="btn btn-default" /> 
    </div> 
</div> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

EmployeeViewModel

答えて

0

あなた現在のコード(編集ビュー)は、以下のような入力マークアップを生成します。

<input class="form-control text-box single-line" 
     id="item_DepartmentName" name="item.DepartmentName" type="text" value="SomeName" /> 

name属性値を参照してください。それは"item.DepartmentName"です。あなたのHttpPostアクションメソッドのパラメータは、この名前のプロパティを持たないEmployeeViewModelタイプです!したがって、モデルバインダーは、この名前("item.DepartmentName")を使用してフォームから投稿されたフォームデータを、メソッドパラメーターのプロパティにマップすることはできません。

解決策は、入力フィールドのの名前の属性値をEmployeeViewModelのプロパティ名と一致するように更新することです。理想的には、コードは以下のようなマークアップを生成する必要があります。

<input name="DepartmentName" type="text" value="Some value" /> 

htmlヘルパーを呼び出すときに、name属性を明示的に指定することができます。フォームを送信する場合たとえば、Html.TextBoxForヘルパー

@Html.TextBoxFor(i => item.DepartmentName, 
           new { @class = "form-control", NAME = "DepartmentName" }) 

で今、モデルバインダーは、あなたのEmployeeViewModelオブジェクトのDepartmentNameプロパティに、このフォームフィールドの値をマッピングすることができるようになります。

+0

応答のためのShyju - ありがとう。私はまだ失われています。 DepartmentNameは私にとってはうまく見えます。私は行方不明の何か他にありますか?それは別のクラス(学科)であるため、 "EditorFor"よりも "TextBoxFor"に変更する必要がありますか? –

+0

ユーザーの入力フィールドを入力したい場合は、 'TextBoXFor'ヘルパーメソッドを使用します。 – Shyju

+0

私は、行をHtml.TextBoxFor(i => item.DepartmentName、new {htmlAttributes = new {@class = "フォームコントロール"}})に変更しました。それでもnullが表示されます。他に何か間違っていますか? –

関連する問題