2014-10-28 62 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>