如何获取<#=Id#>的值当传递给辅助方法时

发布于 2025-01-01 22:23:54 字数 2687 浏览 0 评论 0 原文

我正在将 ASP.NET MVC 3 与 razor 结合使用。我还使用最新版本的 Telerik MVC

我的视图上有一个网格,显示应用程序列表。每个应用程序都有一个状态。我需要编写一个辅助方法来根据每个应用程序的当前状态在网格的每一行中显示链接。如果状态为 1,那么我需要显示一个编辑链接。我的助手看起来像这样:

public static string ActionLinks(this HtmlHelper helper, string grantApplicationId, string grantApplicationStateId)
{
     List<TagBuilder> linkList = new List<TagBuilder>();
     string actionLinks = string.Empty;

     if (grantApplicationStateId == "1")
     {
          // Edit link
          TagBuilder editLink = new TagBuilder("a");
          editLink.MergeAttribute("href", "/GrantApplication/Edit/" + grantApplicationId);
          editLink.InnerHtml = "Edit";
          linkList.Add(editLink);
     }

     foreach (TagBuilder link in linkList)
     {
          actionLinks += link.ToString() + "<br>";
     }

     return actionLinks;
}

我的 Telerik 网格中的网格列看起来像这样:

column.Bound(x => x.Id)
     .ClientTemplate(@Html.ActionLinks("<#= Id #>", "<#= GrantApplicationStateType.Id #>"))
     .Title("Actions");

我的视图模型看起来像:

public class GrantApplicationListViewModel
{
     // Just partial properties
     public GrantApplicationStateType GrantApplicationStateType { get; set; }
}

并且 GrantApplicationStateType 看起来像:

public class GrantApplicationStateType : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
}

当调用上面的助手方法时, grantApplicationStateId 的值是“<#= GrantApplicationStateType .Id#>”。我如何获得传递的值?我的意思是,如果传递的值为 1,那么我如何获得 1,因为当前它是“<#= GrantApplicationStateType.Id #>”?

更新 2012-02-06

我尝试了 Darin 的链接,在我的代码中使用了完全相同的示例,但更改了以下内容:

column.ActionLink("Open", "Edit", "GrantApplication", item => new { id = item.Id, applicationStateId = item.GrantApplicationStateType.Id });

我需要传递 2 个值。我需要对授予应用程序状态 ID 执行几个 if 语句,然后将特定操作链接返回给客户端。但在循环遍历以下值时失败:

if (memberExpression.Expression is ParameterExpression)
     value = string.Format("<#= {0} #>", memberExpression.Member.Name);
else
     value = GetValue(memberExpression);

传入的第一个参数遍历 if 语句的第一个/true 部分:

value = string.Format("<#= {0} #>", memberExpression.Member.Name);

..但第二个参数遍历 if 的 false 部分:

value = GetValue(memberExpression);

2 之间有什么区别?

然后它在 GetValue 方法中失败,并显示以下消息:

variable 'item' of type MyProject.ViewModels.GrantApplicationListViewModel' referenced from scope '', but it is not defined

我无法让它工作,我寻找了更多示例,但找不到任何示例。

I am using ASP.NET MVC 3 with razor. I am also using the latest version of Telerik MVC.

I have a grid on my view displaying a list of applications. Each application has a state. I need to write a helper method to display links in each row of the grid depending on each application's current state. If the state is 1 then I need to display an Edit link. The helper that I have looks like this:

public static string ActionLinks(this HtmlHelper helper, string grantApplicationId, string grantApplicationStateId)
{
     List<TagBuilder> linkList = new List<TagBuilder>();
     string actionLinks = string.Empty;

     if (grantApplicationStateId == "1")
     {
          // Edit link
          TagBuilder editLink = new TagBuilder("a");
          editLink.MergeAttribute("href", "/GrantApplication/Edit/" + grantApplicationId);
          editLink.InnerHtml = "Edit";
          linkList.Add(editLink);
     }

     foreach (TagBuilder link in linkList)
     {
          actionLinks += link.ToString() + "<br>";
     }

     return actionLinks;
}

The grid column in my Telerik grid looks like this:

column.Bound(x => x.Id)
     .ClientTemplate(@Html.ActionLinks("<#= Id #>", "<#= GrantApplicationStateType.Id #>"))
     .Title("Actions");

My view model looks like:

public class GrantApplicationListViewModel
{
     // Just partial properties
     public GrantApplicationStateType GrantApplicationStateType { get; set; }
}

And GrantApplicationStateType looks like:

public class GrantApplicationStateType : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
}

When the above helper method is called then the value of grantApplicationStateId is "<#= GrantApplicationStateType.Id #>". How would I get the value that was passed through? What I mean is, if the value is 1 that was passed through, how would I get 1 because currently it is "<#= GrantApplicationStateType.Id #>"?

UPDATE 2012-02-06

I tried Darin's link, used the exact same sample in my code, but changed the following:

column.ActionLink("Open", "Edit", "GrantApplication", item => new { id = item.Id, applicationStateId = item.GrantApplicationStateType.Id });

I need to pass through 2 values. I need to do a couple of if statements on the grant application state id, and then return the specific action links to the client. But it fails when looping through the values in:

if (memberExpression.Expression is ParameterExpression)
     value = string.Format("<#= {0} #>", memberExpression.Member.Name);
else
     value = GetValue(memberExpression);

The first parameter passed in goes through the first/true part of the if statement:

value = string.Format("<#= {0} #>", memberExpression.Member.Name);

..but the second parameter goes through the false part of the if:

value = GetValue(memberExpression);

What's the difference between the 2?

And then it fails at the GetValue method with the following message:

variable 'item' of type MyProject.ViewModels.GrantApplicationListViewModel' referenced from scope '', but it is not defined

I can't get this to work, and I looked for some more samples and couldn't find any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2025-01-08 22:23:54

您无法使用助手来完成此操作。在 ASP.NET MVC 帮助程序中,运行在服务器上。注意到 Telerik 网格中的 ClientTemplate 名称了吗?这意味着在客户端上运行。

它的作用是简单地使用 <#= Id #> 作为服务器端助手的占位符,该助手将生成一些 HTML 并在客户端, Telerik grid 将进行简单的字符串替换,以便放置仅客户端知道的值。

当您的服务器端 ActionLinks 帮助程序被调用时,Telerik 网格无法向您传递仅在客户端上已知的实际值。

您可以查看以下博客文章 一个很好的扩展。

You can't do this using a helper. In ASP.NET MVC helpers run on the server. Notice the ClientTemplate name in the Telerik grid? That is meant to run on the client.

What it does is that it simply uses <#= Id #> as a placeholder to a server side helper which will generate some HTML and on the client side, the Telerik grid will do a simple string replace in order to put the value which is only known on the client.

At the moment your server side ActionLinks helper is invoked, the Telerik grid cannot pass you the actual value which is known only on the client.

You may take a look at the following blog post for a nice extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文