2012-05-13 6 views
0

私は、jqGrid編集フォームのdataurlから選択リストを塗りつぶすグリッドを持っています。これらの値はgetに正常に取り込まれます。私はjsonとして自分のデータを取り戻しているし、選択リストに表示されているアイテムのフィールドIDと名前を持つLookupItemのカスタムタイプがあります。この部分はうまくいきます。たとえば、LookupItem型のLocationフィールドを持ち、LookupItemにIdとNameフィールドがあるスケジュールされたコース(以下の2番目のコード例)を表すオブジェクトモデルがあります。asp.net mvcサイトの選択コントロールのjqGrid editmodeで返される値を取得するにはどうすればよいですか?

私の問題は、私のActionResultメソッドでも、これらのフィールドがサーバーにポストバックされないということです。私はnullになっています。私はcustom_func関数を追加し、値パラメータが私が探しているIdを保持していることを確認しましたが、ActionResultメソッドに投稿されたIDを取得する方法がわかりません。何か案は?

HTML/jqGridコード:ScheduledCourseため

  $("#grid").jqGrid({ 
      url: 'ABC.Admin/CourseAdmin/GetScheduledCourses/', 
      datatype: 'json', 
      jsonReader: { repeatitems: false }, 
      mtype: 'GET', 
      colNames: ['Id', 'Date', 'LocationId', 'Location', 'TimeId', 'Time', 'CostId', 'Cost', 'InstructorId', 'Instructor', 'Additional Info'], 
      colModel: [ 
       { name: 'ScheduledCourseId', index: 'ScheduledCourseId', width: 40, key: true, hidden: true }, 
       { name: 'ScheduledCourseDate', index: 'ScheduledCourseDate', width: 50, editable: true }, 
       { name: 'Location.Id', index: 'Location.Id', width: 25, editable: false, hidden: true }, 
       { name: 'Location.Name', index: 'Location.Name', width: 150, editable: true, edittype: 'select', 
        editoptions: { 
         dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=locations', 
         buildSelect: createSelectList 
        }, 
        editrules: { 
        custom: true, 
        custom_func: locationCheck 
        } 

       }, 
       { name: 'Time.Id', index: 'Time.Id', width: 25, editable: false, hidden: true }, 
       { name: 'Time.Name', index: 'Time.Name', width: 50, editable: true, edittype: 'select', 
        editoptions: { 
         dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=times', 
         buildSelect: createSelectList 
        } 
       }, 
       { name: 'Cost.Id', index: 'Cost.Id', width: 25, editable: false, hidden: true }, 
       { name: 'Cost.Name', index: 'Cost.Name', width: 50, editable: true, edittype: 'select', 
        editoptions: { 
        dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=costs', 
        buildSelect: createSelectList 
        } 
       }, 
       { name: 'Instructor.Id', index: 'Instructor.Id', width: 25, editable: false, hidden: true }, 
       { name: 'Instructor.Name', index: 'Instructor.Name', width: 100, editable: true, edittype: 'select', 
        editoptions: { 
         dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=instructors', 
         buildSelect: createSelectList      
         } 
       }, 
       { name: 'AdditionalInfo', index: 'AdditionalInfo', width: 200, editable: true, edittype: 'textarea', 
        editoptions: { 
         rows: "6", 
         cols: "40" 
        } 
       }, 
      ], 
      pager: jQuery('#pager'), 
      rowNum: 20, 
      sortname: 'Date', 
      sortorder: "asc", 
      viewrecords: true, 
      caption: 'Scheduled Courses', 
      height: 400, 
      loadonce: true, // needs to be true for client side paging to work 
      autowidth: true, 
      loadtext: 'Loading...' 
     }) 
     $("#grid").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true }, 
      { /* edit options */ 
       url: 'ABC.Admin/CourseAdmin/UpdateScheduledCourse/', 
       closeOnEscape: true, 
       closeAfterEdit: true, 
       width: 450 
      }, 
      { /* add options */ 
       url: 'ABC.Admin/CourseAdmin/CreateScheduledCourse/', 
       closeOnEscape: true, 
       closeAfterAdd: true 
      }, 
      { /* delete options */ 
       url: 'ABC.Admin/CourseAdmin/DeleteScheduledCourse/', 
       closeOnEscape: true 
      } 
     ); 

    }); 

     function locationCheck(value, colname) { 
      debugger; 
      if (value < 0 || value > 20) 
       return [false, "Please enter value between 0 and 20"]; 
      else 
       return [true, ""]; 
     } 

オブジェクトモデル:

public class ScheduledCourse 
    { 
     public ScheduledCourse() { } 
    //for admin scheduled course page 
    public ScheduledCourse(int scheduledCourseId, string scheduledCourseDate, int locationId, string locationName, 
     int timeId, string timeName, int costId, string costName, int instructorId, string instructorName, string additionalInfo) 
    { 
     this.ScheduledCourseId = scheduledCourseId; 
     this.ScheduledCourseDate = scheduledCourseDate; 
     this.Location = new LookupItem(locationId, locationName); 
     this.Time = new LookupItem(timeId, timeName); 
     this.Cost = new LookupItem(costId, costName); 
     this.Instructor = new LookupItem(instructorId, instructorName); 
     this.AdditionalInfo = additionalInfo; 
    } 

    public int ScheduledCourseId { get; set; } 
    public string ScheduledCourseDate { get; set; } 
    public LookupItem Location { get; set; } 
    public LookupItem Time { get; set; } 
    public LookupItem Cost { get; set; } 
    public LookupItem Instructor { get; set; } 
    public string UserName { get; set; } 
    public string AdditionalInfo { get; set; } 
} 
public class LookupItem 
{ 
    public LookupItem(int id, string name) 
    { 
     this.Id = id; 
     this.Name = name; 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
} 

コントローラーコード:ここで

 //colNames: ['Id', 'Date', 'LocationId', 'Location', 'TimeId', 'Time', 'CostId', 'Cost', 'InstructorId', 'Instructor', 'Additional Info'], 
    [HttpPost] 
    public ActionResult UpdateScheduledCourse(string oper, int id, string scheduledCourseDate, string location, string locationId, string timeId, 
     string timeName, string costId, string costName, string instructorId, string instructorName, string additionalInfo) 
    { 
     Models.ScheduledCourseProvider p = new Models.ScheduledCourseProvider(); 

     //Models.ScheduledCourse o = new Models.ScheduledCourse(
     // id, scheduledCourseDate, Convert.ToInt32(locationId), locationName, Convert.ToInt32(timeId), timeName, Convert.ToInt32(costId), 
     // costName, Convert.ToInt32(instructorId), instructorName, additionalInfo); 

     //bool saved = p.UpdateScheduledCourse(o); 

     bool saved = false; 
     if (!saved) 
     { 
      Response.StatusCode = 500; 
      return Content("Record not saved!"); 
     } 
     else 
     { 
      return Json(false); 
     } 
    } 

は、サーバーに渡されるのparamsですActionResultメソッドがコントローラで呼び出されます(http:// localhost:30320/OrchardLocal/ABC.Admin/CourseAdmi N/UpdateScheduledCourse /) - 私はLookupItemの共有タイプを使用していますので、ドット表記法が使用されて気づく:すべての

ScheduledCourseDate=07%2F01%2F2011&Location.Name=10016&Time.Name=1014&Cost.Name=1001&Instructor.Name=10001&AdditionalInfo=blahblahblah&oper=edit&id=12126 
+0

で名前を使用することができます...私はのActionResultメソッドにFormCollectionタイプを使用することができるようになります。 – Tone

答えて

1

まずあなたが内部のドットを持たないために、列のnameプロパティを変更する必要があります。たとえば、代わりにアンダースコアを使用できます。また、'Location.Id''Time.Id''Instructor.Id'が必要かどうかはわかりません。サーバー上で必要な場合はname: 'location', jsonmap: 'Location.Name'を使用できます。同じ方法でname: 'ScheduledCourseDate'の代わりにname: 'scheduledCourseDate', jsonmap: 'ScheduledCourseDate'を使用することができます。

変更後、選択肢から選択されたIDは、短い名前の 'location'、 'time'、 'instructor'によってポストされます。だから、この上でいくつかの進歩を遂げてUpdateScheduledCourse方法

[HttpPost] 
public ActionResult UpdateScheduledCourse(string oper, int id, string scheduledCourseDate, 
    string location, string time, string cost, string instructor, string additionalInfo) 
+0

素晴らしい!私はjsonmapプロパティを知らなかった。私はサーバ上のドット表記が必要です。これらは強く型付けされたオブジェクトです。 IdとNameはLocation、Cost、Time、およびその他の検索項目のプロパティです。 インデックスプロパティはjsonmapにも一致する必要がありますか? – Tone

+1

@Tone:サーバーに送信される 'sidx'パラメータとして持つ名前に' index'を指定する必要があります(to: 'index'を指定する必要があります。 '' ABC.Admin/CourseAdmin/GetScheduledCourses/'')を実行します。現在、 'sortname: 'Date''を使用しています。だからあなたは 'sidx = Date'を持っています。ユーザが '' Location''カラムのヘッダをクリックすると、 'sidx = Location.Name'で新しいリクエストが送信されます。 'name: 'location''を' index'なしで使用すると、ソート時に 'sidx = location'という値が使われます。あなたが 'sidx = blabla.location'を望むなら、' index: 'blabla.location''を含めるべきです。プロパティ 'index'は' sidx'にのみ使われます。 – Oleg

関連する問題