技术开发 频道

ASP.NET MVC3新功能之WebGrid查询绑定

  现在我们多了两个查询条件,叫Student Name,NRIC/Password No.,如下图所示:

1
 

  那么当我们点击Sorting的时候如何它才能保持现有的查询条件不变呢?我所采取的策略是使用TempData保存现有的查询条件,所以我们就需要在Action中获取查询条件并将其放放TempData中,当Action返回时,我们在view中把现有的TempData再次的进行绑定,因为WebGrid的Sorting他请求的是一个URL,而不是提交事件,所以当点击Sorting的时候,他并不会收集form中的对象,但是因为之前的一次查询,我们将查询条件存入了TempData中,所以现在我们可以从TempData中取出查询条件,Action代码修改如下:

   public ActionResult Gridview()
         {
             IDictionary
<string, string> searchConditions = new Dictionary<string, string>();
  
            
if (this.Request.Form.AllKeys.Length > 0)
             {
                 searchConditions.Add(
"StudentName", Request["StudentName"]);
                 searchConditions.Add(
"nricPassport", Request["nricPassport"]);
            }
            
else
             {
                
object values = null;

                
if (this.TempData.TryGetValue("SearchConditions", out values))
                 {
                     searchConditions
= values as Dictionary<string, string>;
                 }
             }

             this.TempData[
"SearchConditions"] = searchConditions;

            
string studentName = GetSearchConditionValue(searchConditions, "StudentName");
            
string nric = GetSearchConditionValue(searchConditions, "nricPassport");

             DALDataContext da
= new DALDataContext();
             var result
= (from s in da.T_STUDENTs
                           where (
string.IsNullOrEmpty(studentName) || s.StudentName.StartsWith(studentName))
                          
&& (string.IsNullOrEmpty(nric) || s.NRICPassport.StartsWith(nric))
                          
select s).ToList();
             this.ViewData.Model
= result;
             return View();
         }

        
private static string GetSearchConditionValue(IDictionary<string, string> searchConditions, string key)
         {
            
string tempValue = string.Empty;

            
if (searchConditions != null && searchConditions.Keys.Contains("StudentName"))
             {
                 searchConditions.TryGetValue(key, out tempValue);
             }
             return tempValue;
         }

 

  上在的Action就是实现了这样的一件事情,第一次提交的时候把查询条件给TempData,然后在后面的非Submit事件中,使用已经保存在TempData中的查询条件。这样我们就实现了查询条件与MVC WebGrid的绑定。虽然这样的方案并不是十分的完美,但也是其解决方案之一,如果有那个博友有更好的解决方案,还请拍砖斧正。

  上面我们已经讲完了本文主要的3点内容,但是WebGrid中还有一些其它的参数,很多文章都有介绍,在这里我们再多做一次补充,以方便博友阅读此文。

  但是通过上文的实例,我们个人觉的他有一些不足,因为他在取Data的时候,一次性的返回所有需要的 Data,然后再将其返回到我们的Action中,这样必然增加了Application与Database之间数据的通信量,如果数据量很大的时候必然也会给网络带来很大的压力,如果我们在Database里就把分页的数据直接返回,那么将大大减少数据传输量(数据库分页也会比较快)。

  三、方法及参数说明

  WebGrid有两个构造函数,在他的构造函数中所需要的一些参数正是用来指定所创建出来的WebGrid生成的HTML是否具备如分页、排序等功能。主要参数如下:

// Methods
      
public WebGrid([Dynamic(new bool[] { false, true })] IEnumerable<object> source, [Optional, DefaultParameterValue(null)] IEnumerable<string> columnNames,
[Optional, DefaultParameterValue(
null)] string defaultSort,
[Optional, DefaultParameterValue(
10)] int rowsPerPage,
[Optional, DefaultParameterValue(
true)] bool canPage,
[Optional, DefaultParameterValue(
true)] bool canSort,
[Optional, DefaultParameterValue(
null)]
string ajaxUpdateContainerId,
[Optional, DefaultParameterValue(
null)]
string fieldNamePrefix,
[Optional, DefaultParameterValue(
null)]
string pageFieldName,
[Optional, DefaultParameterValue(
null)]
string selectionFieldName,
[Optional, DefaultParameterValue(
null)]
string sortFieldName,
[Optional, DefaultParameterValue(
null)]
string sortDirectionFieldName);
internal WebGrid(HttpContextBase context, [Dynamic(
new bool[] { false, true })] IEnumerable<object> source, [Optional, DefaultParameterValue(null)] IEnumerable<string> columnNames,
[Optional, DefaultParameterValue(
null)]
string defaultSort,
[Optional, DefaultParameterValue(
10)] int rowsPerPage,
[Optional, DefaultParameterValue(
true)] bool canPage,
[Optional, DefaultParameterValue(
true)] bool canSort,
[Optional, DefaultParameterValue(
null)]
string ajaxUpdateContainerId,
[Optional, DefaultParameterValue(
null)]
string fieldNamePrefix,
[Optional, DefaultParameterValue(
null)]
string pageFieldName,
[Optional, DefaultParameterValue(
null)]
string selectionFieldName,
[Optional, DefaultParameterValue(
null)]
string sortFieldName,
[Optional, DefaultParameterValue(
null)]
string sortDirectionFieldName);
  
    
// Properties
      
public string AjaxUpdateContainerId { get; }
    
public IEnumerable<string> ColumnNames { get; }
     [Dynamic(
new bool[] { false, true })]
    
public IEnumerable<object> DataSource { [return: Dynamic(new bool[] { false, true })] get; }
    
private Type ElementType { get; }
    
public string FieldNamePrefix { get; }
    
public bool HasSelection { get; }
    
private HttpContextBase HttpContext { get; }
    
public int PageCount { get; }
    
public string PageFieldName { get; }
    
public int PageIndex { get; set; }
    
private NameValueCollection QueryString { get; }
    
public IList<WebGridRow> Rows { get; }
    
public int RowsPerPage { get; }
    
public int SelectedIndex { get; set; }
    
public WebGridRow SelectedRow { get; }
    
public string SelectionFieldName { get; }
    
public string SortColumn { get; set; }
    
public SortDirection SortDirection { get; set; }
    
public string SortDirectionFieldName { get; }
    
public string SortFieldName { get; }
    
public int TotalRowCount { get; }

  

0
相关文章