2017-10-08 4 views
0

にビューからメンバーオブジェクトまたはメンバーのリストを持つオブジェクトを渡しますここに私の図である。ここではは、コントローラ

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using NHibernate.AspNet.Identity; 

    namespace MyProject.Web.Models 
    { 
     public class IdentityRoleView 
     { 
      public virtual string Id { get; set; } 
      public virtual string Name { get; set; } 
      public virtual IList<IdentityUser> Users { get; set; } 
     } 
    } 

は私のコントローラである:ここでは

[HttpGet] 
      public ActionResult Edit(string roleId) 
      { 
       IdentityRole role = Service.Find(roleId); 

       return View("Edit", AutoMapper.Mapper.Map<IdentityRoleView>(role)); 
      } 

      [HttpPost] 
      public ActionResult Edit(IdentityRoleView role) 
      { 
       Service.Update(role); 
       TempData["Comment"] = "The record was updated"; 
       return RedirectToAction("Index"); 
      } 
     } 

が私の見解です。

@model MyProject.Web.Models.IdentityRoleView 
@{ 
    ViewBag.Title = "Edit"; 
    Layout = "~/Areas/Administration/Views/Shared/_Layout.cshtml"; 
} 

    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     @Html.HiddenFor(model => model.Id); 
     <div> 
      Role name 
     </div> 
     <p> 
      @Html.TextBoxFor(model => model.Name) 
     </p> 

     for (int items = 0; items < Model.Users.Count; items++) 
      { 
       @Html.DisplayFor(m => m.Users[items].Id); 
       @Html.DisplayFor(m => m.Users[items].UserName); 
      } 

     <input type="submit" value="Save" /> 
    } 

IdentityRoleView.Usersは、コントローラに戻すと常にnullです。何が問題ですか? IdentityRoleView.Usersがビューに表示されています。IdentityRoleView.Usersが常にnullであるため、サーバーにポストバックされるとすべて削除されます。ここで

+2

あなたのフォームにそのリストのフォーム値が存在しないので、少なくとも今は投稿していません。あなたが持っている唯一のフォーム値は、 "名前"プロパティです。モデルのリストにモデルバインダーを使用する方法については、私はそれのための既製のコードはありませんが、これは始めるのに最適な場所のようです:https://stackoverflow.com/questions/19964553/mvc-オブジェクトのリストの後にフォームできない – David

+0

これで、現在の値をコントローラのPOSTメソッドに戻したいだけですか?それですか? 明確にしようとしています。 –

答えて

0

ユーザーが読み取り専用TextBoxFor行い、値を編集することはできませんことをしたい場合のviewmodel

public class IdentityRoleView 
    { 
     public IdentityRoleView() 
     { 
      Users = new List<IdentityUser>(); 
     } 
     public virtual string Id { get; set; } 
     public virtual string Name { get; set; } 
     public virtual IList<IdentityUser> Users { get; set; } 
    } 

[コントローラのメソッド

public ActionResult Edit(string roleId) 
     { 
      IdentityRoleView model = new IdentityRoleView(); 

      model.Name = "IdentityRoleViewUser"; 
      model.Id = "2"; 

      model.Users.Add(new IdentityUser { 
       UserName = "testuser", 
       Id = "1", 
       Email = "[email protected]" 
      }); 

      model.Users.Add(new IdentityUser 
      { 
       UserName = "testuser2", 
       Id = "2", 
       Email = "[email protected]" 
      }); 

      return View("Edit", model); 
     } 

     [HttpPost] 
     public ActionResult Edit(IdentityRoleView model) 
     { 

      //Your logic 
      return RedirectToAction("Index"); 
     } 

はその後

@model MyProject.Web.Models.IdentityRoleView 
@{ 
    ViewBag.Title = "Edit"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    @Html.HiddenFor(model => model.Id); 
    <div> 
     Role name 
    </div> 
     <p> 
      @Html.TextBoxFor(model => model.Name) 
     </p> 

for (int i = 0; i < Model.Users.Count(); i++) 
{ 
    @Html.TextBoxFor(model => model.Users[i].Email) 
    @Html.TextBoxFor(model => model.Users[i].UserName) 

} 

      <input type="submit" value="Save" /> 
} 

を表示しています。

+0

詳しいことはありますか?私がこれをするとき。コレクションはHttpPost Controllerメソッドではnullです。 – w0051977

0

これはMVCのちょっとした問題です。

あなたの持っているものはほとんど正しいです。

お使いのコントローラだけで結構です。しかし

[HttpGet] 
     public ActionResult Index() 
     { 
      Person p1 = new Person(); 
      p1.name = "Ian"; 

      List<Sport> sports = new List<Sport>(); 
      Sport s1 = new Sport(); 
      s1.description = "Football"; 
      sports.Add(s1); 
      Sport s2 = new Sport(); 
      //s1.description = "Netball"; I'm guessing this is a typo? 
      s2.description = "Netball"; 
      sports.Add(s2); 
      p1.sports = sports; 

      return View("Person", p1); 
     } 

     [HttpPost] 
     public ActionResult Index(Person p1) 
     { 
      return View(); 
     } 

、問題があり、そのビュー:

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

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

     @* This is the key difference here *@ 
     @for (int items = 0; items < Model.sports.Count; items++) 
     { 
      <div> 
       @Html.DisplayFor(sport => Model.sports[items].description) 
       @Html.HiddenFor(sport => Model.sports[items].description) 
       <hr /> 
      </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 List", "Index") 
</div> 

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

だから、私が説明しましょう。

あなたのスポーツリストをForm(){}に移動しました。これは、実際にフォームにオブジェクトを戻すために行う必要があります。そのフォームにあるものが何であれ、それがポストバックするものです。

もう1つのことは、実際にモデルオブジェクトをバインドするためのフォームを与えることです。 @Html.DisplayForヘルパーはそれを行いません。ユーザーがスポーツを変更しないように思われるので、単に@Html.HiddenForを作成してください。これにより、値が[HttpPost] Index(Person p1)メソッドに返されます。

それ以降は、あなた次第です。

これが役に立ちます。

+0

申し訳ありませんが、これは機能しません。構文エラーがあります。一度すべての構文エラーを修正しました。コレクションは戻されません。ヌルです。 – w0051977

+0

興味深い...それは私のためにコンパイルされ、うまくいったからです... –