2014-10-28 59 views
0

我有一個ViewModel類:[ASP MVC]:在視圖模型的鬆散值列表提交

public class IndexViewModel 
    { 
     public IndexViewModel() 
     { 
      GridList = new List<GridModel>(); 
     } 
     public List<Models.GridModel> GridList { get; set; } 
     public string ATempProperty { get; set; }   
    } 

我把它傳給我的看法成功。在提交我想返回到當前ViewModel的行動。但GridList是空的!我該怎麼做才能從視圖中獲取列表? 我的看法是這樣的:

@using (Html.BeginForm("Edit", "Home", FormMethod.Post)) 
{ 
    <div id="lll"> 
     @Html.TextBoxFor(m => m.ATempProperty)   
     @Html.HiddenFor(m => m.GridList) 
    </div> 

    <input type="submit" value="Submit" /> 
} 
+0

你需要渲染GridList'的'每個屬性在'for'循環中,但是如果它們都是隱藏的控件,它又有什麼意義呢(一般來說性能更好,只需從後臺獲取集合即可) – 2014-10-28 06:32:40

+0

爲什麼你想發送數據回服務器已經有服務器?數據是否在客戶端更改,如果是隻發送更改的數據?我希望你意識到重點是接收來自USER的輸入並將該數據發送到服務器。 – SBirthare 2014-10-28 07:35:37

回答

0

我做到了。但是出現錯誤消息: 沒有爲此對象定義的無參數構造函數。 我只有一個動作是:

public ActionResult Edit(IndexViewModel ind) 
     {  
      Do s.t .  
      return View(); 
     } 

我的視圖模型爲:

public class GridModel 
    { 
     //public GridModel() { } 
     public GridModel(List<object> GridDataSource, string GridName, int RecordCount, 
      int PageRecordCount, int CurrentPageIndex, string Controller, string ActionName, string TargetID) 
     { 
      this.GridDataSource = GridDataSource; 
      this.GridName = GridName; 
      this.RecordCount = RecordCount; 
      this.PageRecordCount = PageRecordCount; 
      this.CurrentPageIndex = CurrentPageIndex; 
      this.Controller = Controller; 
      this.ActionName = ActionName; 
      this.TargetID = TargetID; 


     } 

     public List<string> classNameList = new List<string>(); 

     public List<object> GridDataSource { get; set; } 
     public string GridName { get; set; } 
     public int RecordCount { get; set; } 
     public int PageRecordCount { get; set; } 
     public int CurrentPageIndex { get; set; } 
     public string Controller { get; set; } 
     public string ActionName { get; set; } 
     public string TargetID { get; set; } 

     private Dictionary<string, string> styles = 
      new Dictionary<string, string>(); 

     private Dictionary<string, string> columns= 
      new Dictionary<string, string>();   

     [ReadOnly(true)] 
     public enum GridClasses 
    { 
      GridTable, 
      GridHeader, 
      GridBody, 
      GridFooter, 
      GridAlternativeRow, 
      GridPager 
    } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="GridClass">Chose yor class from GridModel.GridClass Enum</param> 
     /// <param name="CssStyle">Enter full css attribute and values in a string format. Something like this : 
     /// "color:red; font-size:10px;"</param> 
     public void SetGridStyle(GridModel.GridClasses GridClass, string CssStyle) 
     { 
      if (styles.ContainsKey(GridClass.ToString())) 
       styles[GridClass.ToString()] = CssStyle; 
      else 
       styles.Add(GridClass.ToString(), CssStyle); 
     }  

     /// <summary> 
     /// 
     /// </summary> 
     /// <returns>Return: The CSS style of getted class in CSS originall format.</returns> 
     public string GetGridStyle(GridModel.GridClasses GridClass) 
     { 
      if (styles.ContainsKey(GridClass.ToString())) 
      { 
       return " GridContainer" + GridName + " ." + GridClass.ToString() + "{" + styles[GridClass.ToString()] + "}"; 
      } 
      else 
       return ""; 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <returns>Return: The CSS style of all classes of grid in CSS originall format.</returns> 
     public string GetGridStyle() 
     { 
      string css = ""; 
      foreach(KeyValuePair<string, string> d in styles) 
       if(d.Key == GridClasses.GridAlternativeRow.ToString()) 
        css += " #GridContainer" + GridName + " ." + GridName + d.Key + " tr:nth-child(even){" + d.Value + "} "; 
       else if (d.Key == GridClasses.GridPager.ToString()) 
       css += " #GridContainer" + GridName + " ." + GridName + d.Key + " span{" + d.Value + "} "; 
       else 
        css += " #GridContainer" + GridName + " ." + GridName + d.Key + "{" + d.Value + "} "; 
      return css; 
     } 

     public void SetColumnsStyle(string ColumnName, string CssStyle) 
     { 
      if (columns.ContainsKey(ColumnName)) 
       columns[ColumnName] = CssStyle; 
      else 
       columns.Add(ColumnName, CssStyle); 
     } 

     public string GetColumnStyle() 
     { 
      string css = ""; 
      foreach (KeyValuePair<string, string> d in columns) 
       css += " #GridContainer" + GridName + " ." + GridName + d.Key + "{" + d.Value + "} "; 
      return css; 
     } 

     public List<string> GetStyledColumns() 
     { 
      List<string> l = new List<string>(); 
      foreach (KeyValuePair<string, string> d in columns) 
       l.Add(d.Key); 
      return l; 
     } 

     public string EditController = ""; 
     public string EditAction = ""; 
     public bool IsInEditMode = false; 
     public string GridCssClass = "webgrid"; 

    } 

而且我的新觀點是:

@using (Html.BeginForm("Edit", "Home", FormMethod.Post, Model)) 
{ 
    int listNumber = 0; 
    <div id="lll"> 

     @Html.TextBoxFor(m => m.ATempProperty)  
     @foreach(WebApplication1.Models.GridModel l in Model.GridList) 
     { 
      @Html.HiddenFor(m => m.GridList[listNumber].GridName); 
      @Html.HiddenFor(m => m.GridList[listNumber].PageRecordCount);    
     }   
    </div> 

    <input type="submit" value="Submit" /> 
} 
+0

你必須有一個無參數的構造函數,即'public GridModel(){...};'而且你的代碼不會工作。它需要是'for(int i = 0; i m.GridList [i] .GridName)...}'。但正如我之前所說,這樣做並不重要,你只是打開自己被黑客入侵併降低你的網站性能。因爲你不改變'GridModel'屬性的任何值(它們都是隱藏的輸入),所以當你回發 – 2014-10-28 07:14:01

+0

時,再次從數據庫中獲取它們。你爲什麼要做'var s = Request.Form [「ATempProperty」]; '? 'ind.ATempProperty'已經包含了值!你爲什麼要做'this.UpdateModel(ind);'?該模型已被綁定!我建議學習MVC的基礎知識的時間是有序的。 – 2014-10-28 07:19:20

+1

謝謝Stephen,var s = Request.Form [「ATempProperty」];只是爲了測試。其實我正在爲MVC.net定製一個網格。所以我有一個列表作爲網格數據源,我通過反射來獲取屬性。所以我不能將它們綁定到網格上的可編輯字段。另一方面,我需要保持網格屬性,如名稱,Pagerecourde EditableRecordList,...。所以保持這些的方法是支持他們控制並使用FormCollection來檢索網格。 – 2014-10-28 07:29:24

2

你必須遍歷並創建隱藏字段屬性:

<div id="lll"> 
     @Html.TextBoxFor(m => m.ATempProperty) 
     @for(int i = 0 ; i< Model.GridList.Count; i++) 
     {   
      @Html.HiddenFor(m => Model.GridList[i].PropertyA) 
      @Html.HiddenFor(m => Model.GridList[i].PropertyB) 
      ............................ 
      ............................ 
     } 
    </div>