无法将lambda表达式转换为字符串类型,因为它不是委托类型

时间:2012-12-13 17:54:11

标签: c# asp.net-mvc lambda

我正在尝试使用以下结构/代码创建一个链接按钮:

<%:school => string.Format("<a class='add' href='{0}' title='Add {1} to {4}'><img class='pic2' alt='{2}' src='{3}'/></a>",
                                          Url.Action("AddSchoolToParty", "PartySchool",
                                          new { partyId = Model.PartyId, schoolId = school.SchoolId }),
                                          School.EntityName, string.Empty,
                                          Url.Content("~/Content/images/addImage.png"),
                                          School.EntityName)%>

我得到的错误是:

CS1660:无法将lambda表达式转换为'string'类型,因为它不是委托类型

这也是使用我拥有的信息创建链接的正确格式吗?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Kids.MVC.Models.ViewModels.SchoolFormViewModel>" %>

<%@ Import Namespace="Kids.Resources" %>
<%@ Import Namespace="Kids.Resources.Entities" %>
<%@ Import Namespace="MvcContrib.Pagination" %>
<%@ Import Namespace="MvcContrib.UI.Grid" %>
<%@ Import Namespace="MvcContrib.UI.Pager" %>
<%: Html.ValidationSummary(true, Message.ValidationErrorSummary, new{@class= "error"}) %>
<%=Html.DatePickerEnable() %>

这是我在不同页面中使用的格式,这就是我想要在没有列和string.format的标签中重写的按钮链接 - 我想知道重写此链接的正确方法

column.For(
                        school =>
                        string.Format("<a class='add' href='{0}' title='Add {1} to {4}'><img class='pic2' alt='{2}' src='{3}'/></a>",
                                      Url.Action("AddSchoolToParty", "PartySchool",
                                      new { partyId = Model.PartyId, schoolId = school.SchoolId }),
                                      School.EntityName, string.Empty,
                                      Url.Content("~/Content/images/addImage.png"),
                                      School.EntityName)).Encode(false).Sortable(false);

1 个答案:

答案 0 :(得分:1)

试试这样:

<a class="add" href="<%: Url.Action("AddSchoolToParty", "PartySchool", new { partyId = Model.PartyId, schoolId = Model.SchoolId }) %>" title="<%: string.Format("Add {0} to {0}", School.EntityName) %>">
    <img class="pic2" alt="" src="<%: Url.Content("~/Content/images/addImage.png") %>" />
</a>

但为了避免这种丑陋的标签汤,我建议你编写一个自定义HTML帮助器,这将使这种语法更加清晰。