【问题标题】:MVCContrib grid - select rowMVCContrib 网格 - 选择行
【发布时间】:2011-02-23 20:47:35
【问题描述】:

我有一个 MVCContrib 网格,它显示来自 Account 对象的选定属性。我希望用户选择一行并被带到另一个页面以查看他们单击的行所代表的对象的完整属性。如何将 .Selected 动作添加到网格的行?

【问题讨论】:

    标签: model-view-controller asp.net-mvc-2 mvccontrib


    【解决方案1】:

    我今天刚遇到一个类似的问题。

    您可以像这样使用 .RowAttributes:

    Html.Grid(Model).Columns(column => 
    {
        column.For(e => e.Id); 
        column.For(e => e.Message); 
    })
    .RowAttributes(x => new Dictionary<string, object>
        {{"onClick", "window.location.href = 'google.com'"}})
    .Render();
    

    结果,当您单击 a 时,它会触发 javascript“onclick”并打开 google。您可以使用 Lamda 中的“x”更改 url 以传入一个 Id。

    【讨论】:

      【解决方案2】:

      如果您在 MVC3 上下文中使用 Grid,您还可以通过服务器端的扩展类来完成此操作:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using MvcContrib;
      using MySolution.ViewModels;
      
      namespace MySolution.Models.Extensions
      {
      public static class RowAttributeExtensions
      {
          public static Hash GetRowAttributes(this MySolution.ViewModels.Model)
          {
      
              string onclickFunctionBody = "{window.location.href = '/MyController/MyAction?id=" + Model.Id + "'; return false;}";
              Hash hash = new Hash(onclick => onclickFunctionBody)
              return hash;
          }
      }
      }
      

      在客户端,这将采取以下形式:

      @Html.Grid(Model).RowAttributes(row => row.Item.GetRowAttributes()).Columns(column =>
      {
          column.For(c => c.Col1);
          column.For(c => c.Col2);
          ...
      })
      

      【讨论】:

        猜你喜欢
        • 2012-10-02
        • 1970-01-01
        • 2011-07-22
        • 2010-10-24
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        相关资源
        最近更新 更多