2017-03-27 4 views
0

アプリケーションのユーザープロファイルページを作成しています。このページでは、基本データフィールド(姓、名など)を表示および編集できます。また、リンクされているユーザーのグリッドもあります。ユーザーは、このグリッドに対して基本的なCRUD操作を実行できるはずです。このグリッドをviewmodelのプロパティとしてビューに渡されるIEnumerable(または同等のもの)にバインドするにはどうすればよいですか?ビューのviewmodelでIEnumerableにグリッドをバインドします

ビューモデルは次のようなものです:私のユーザープロファイルビューは、現在、以下のようになります

using System.Collections.Generic; 

namespace Pricing.Models.ViewModel 
{ 
    public class UserProfileUpdateViewModel 
    { 
     public int UserId { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public IEnumerable<LinkedUserViewModel> LinkedUsers { get; set; } 
    } 
} 

。現在壊れているものを示す#38のTODOコメントに注意してください。

@model Pricing.Models.ViewModel.UserProfileUpdateViewModel 
<div class="panel-body"> 
    <h2>User Profile</h2> 
    @using (Html.BeginForm("UpdateProfile", "Account", FormMethod.Post)) 
    { 
     @Html.AntiForgeryToken() 
     @Html.HiddenFor(profile => profile.UserId) 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       <label class="control-label col-md-2">Username</label> 
       <div class="col-md-10" style="padding-top:7px"> 
        @User.Identity.Name 
       </div> 
      </div> 
     </div> 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       @Html.LabelFor(profile => profile.FirstName, "First Name", new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(profile => profile.FirstName, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(profile => profile.FirstName, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
     </div> 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       @Html.LabelFor(profile => profile.LastName, "Last Name", new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(profile => profile.LastName, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(profile => profile.LastName, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
     </div> 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       @Html.LabelFor(profile => profile.LinkedUsers, "Linked Users", new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        //TODO: This bind fails, I need to bind to the IENumerable property of UserProfileUpdateViewModel 
        @(Html.Kendo().Grid<Freds.Retail.DirectPricing.Models.ViewModel.UserProfileUpdateViewModel>() 
         .Name("LinkedUsers") 
         .ToolBar(tools => 
         { 
          tools.Create().Text("Add Linked User"); 
         }) 
         .Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom).DisplayDeleteConfirmation(false)) 
         .Columns(columns => 
         { 
          columns.Bound(p => p.LinkedUsersId).Visible(false).ClientTemplate("#= Id #" + 
          "<input type='hidden' name='LinkedUsers[#= index(data) #].Id' value='#= Id #' />" 
          ); 
          columns.Bound(p => p.LinkedUserName).Visible(false).ClientTemplate("#= LinkedUserName #" + 
          "<input type='hidden' name='LinkedUsers[#= index(data) #].LinkedUserName' value='#= LinkedUserName #' />" 
          ); 
          columns.Command(command => command.Destroy()).Width(110); 
         }) 
         .Scrollable() 
         .Sortable() 
         .HtmlAttributes(new { style = "width:75%" }) 
         .DataSource(source => source 
          .Ajax() 
          .Model(model => 
          { 
           model.Id(d => d.Id); 
          }) 
          .Batch(true) 
          .ServerOperation(false) 
          .Events(events => events.Error("error_handler")) 
          .Read(read => read.Action("LinkedUser_Read", "Account")) 
         ) 
        ) 
       </div> 
      </div> 
     </div> 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       <label class="control-label col-md-2"></label> 
       <div class="col-md-10"> 
        <input type="submit" value="Save Changes" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 
</div> 

答えて

1

型ではなくIEnumerable自体を渡す必要があります。

@(Html.Kendo().Grid<Model.LinkedUsers>() 
+0

ああ。ありがとう、ありがとう。私はそれが理にかなっていると思う。だから、私はUserProfileUpdateViewModelからIEnumerableプロパティを完全に削除する必要がありますか?グリッドがそれ自身のLinkedUsersモデルにバインドされている場合は、IEnumerableプロパティのRead値がnullになると思います。グリッドのデータがUserProfileUpdateViewModelの一部でない場合、グリッド上の作成、更新、または破棄する関数をどのように返すかについてはまだ不明です。 –

+1

@ CollinM.Barrettなぜそれを削除するのですか?リストがレンダリングされないようにするには、リストがnullの場合は単純にチェックし、更新/削除の場合にのみレンダリングします。追加/作成/削除の方法については、http://docs.telerik.com/aspnet-mvc/helpers/grid/editing/server-editingの例を参照してください。 –

関連する問題