在ASP.NET MVC视图助手中,您可以执行类似的操作
<%= Html.ActionLink("click me", "DoSomething", null, new { someAttribute = "a value" } ) %>
Run Code Online (Sandbox Code Playgroud)
这将产生以下HTML
<a href="DoSomething" someAttribute="a value">click me</a>
Run Code Online (Sandbox Code Playgroud)
我的问题是......如果我想设置"class"属性怎么办?
<%= Html.ActionLink("click me", "DoSomething", null, new { class = "a-class-name" } ) %>
Run Code Online (Sandbox Code Playgroud)
那将无法编译,因为"class"是一个保留字.
有解决方法吗?
我用过
Url.Action("actionFoo", "controllerBar")
Run Code Online (Sandbox Code Playgroud)
在我的观点(aspx).但是现在我正在将我的一些标记重构为我创建的HtmlHelper.
问题是我似乎没有包含正确的命名空间,或者View有一些我不知道的默认对象引用.点是编译器找不到Url.Action.
为简单起见,这里是我要做的......
public static MvcHtmlString RenderActionButtons(this HtmlHelper helper, string actionName, string controllerName)
{
TagBuilder customActionButtonTagBuilder = new TagBuilder("a");
customActionButtonTagBuilder.Attributes.Add(new KeyValuePair<string, string>("href", Url.Action(actionName, controllerName)));
customActionButtonTagBuilder.InnerHtml = "foo";
customActionButtonTagBuilder.AddCssClass("custom-action-button");
return MvcHtmlString.Create(customActionButtonTagBuilder.ToString(TagRenderMode.Normal));
}
Run Code Online (Sandbox Code Playgroud)
如何将代码指向正确使用Url.Action?
我想将htmlAttributes作为参数传递给我的HtmlHelper,类似于在Html.ActionLink中创建的("linktext","Home",null,new {width ="100px"})如何传递这个新的{width ="100px"}我的方法
public static string SelectCategoryAdminWithAllItem(this HtmlHelper htmlHelper, string name, **???**)
{ }
Run Code Online (Sandbox Code Playgroud)
以及如何解析它?
谢谢
我正在使用 Razor 和 C# 开发 ASP.NET MVC3 应用程序。我想知道是否有任何方法可以“禁用”使用助手呈现的超链接,而Html.ActionLink不会使用if构造破坏我的视图。至于禁用,我的意思是链接变得不可点击、变灰且没有下划线。
为了使事情清楚,我特此附上我目前用来制作技巧的原样代码。我没有禁用超链接,而是将 N/A 显示为纯文本。
视图部分:
<td>@if(topic.EnableTopicDownloadLink)
{
@Html.ActionLink("Presentation", "Download", new { topicId = topic.TopicId })
}
else
{
@Html.Raw("N/A");
}
</td>
<td>@if (topic.EnableTopicWatchLink)
{
@Html.ActionLink("Video", "Play", new { topicId = topic.TopicId })
}
else
{
@Html.Raw("N/A");
}
</td>
Run Code Online (Sandbox Code Playgroud)
并将 ViewModel 传递给 View
public class TopicDownloadViewModel
{
public int TopicId { get; set; }
public string TopicName { get; set; }
public string TopicPresenter …Run Code Online (Sandbox Code Playgroud) 我查看了可以使用的不同方法
@using (Html.BeginForm())
Run Code Online (Sandbox Code Playgroud)
但我仍然很困惑.我想做的是为它提供一个类名,以便我可以设置自己的参数.
另外我想为这些提供一个课程:
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@Html.ValidationMessageFor(m => m.Login.UserName)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我在 asp.net mvc2 项目中创建了 Html helper 类:
public static class CaptionExtensions
{
public static string Captions(this HtmlHelper helper, Captions captions)
{
var sb = new StringBuilder();
sb.AppendLine("<ul>");
foreach (var caption in captions)
{
// var url = Url.Action("CaptionCategory", new {id = caption.Code} )
sb.AppendLine("<li>");
sb.AppendLine( "<a href="+ url + ">");
sb.AppendLine( caption);
sb.AppendLine( "</a>");
sb.AppendLine("</li>");
}
sb.AppendLine("</ul>");
return sb.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要生成与注释行中的方式类似的 url。注释代码是我在控制器类中的做法,但这是辅助类(静态上下文)。任何帮助???
在MVC3中,我想通过Html.ListBoxFor方法更改HTML输出,以便代替包含所有可用值的HTML列表框(以及突出显示的选定值),我想简单地输出无序列表(UL,LI)所选项目而不是SELECT元素.问题是我希望保持与ListBoxFor方法完全相同的方法签名,接受MultiSelectList对象和List,它是选定的值.然后,我希望无序列表仅输出所选项目值(而不是键)作为UL/LI html.这是我想要的方法签名.如何实现这一目标?
public static MvcHtmlString ListBoxForAsUnorderedList <TModel, TProperty>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList)
Run Code Online (Sandbox Code Playgroud) 我在Helpers.cshtml文件中有很多HTML帮助器,但是一些帮助器(html)需要一些jquery动作,所以我如何在helpers.cshtml中调用jquery,这可能吗?
我知道我们能继续保持页眉或特定页面的js文件,但我不希望做这样的,我只希望这加载特定助手页面上使用jquery或javascript.
有谁有这个想法?我的方案是,我有列表框控件,从helper正确加载,但我需要将自定义主题应用于列表框.
更多的清晰度
//in index.cshtml
@Helpers.testListBox("mylist" "1,2,3,4,5,6,7")
//in Helpers.cshtml
@helper testListBox(string listName, string listData){
//...... HTML code .........
//Javascript here?
}
Run Code Online (Sandbox Code Playgroud) c# model-view-controller html-helper razor-declarative-helpers asp.net-mvc-3
我目前正在将应用程序升级到CakePHP 3.在CakePHP 2中,我使用HTMLHelper :: url生成指向按钮等控制器/操作的链接.
像这样的东西:
<form class="navbar-form navbar-left form-signin" action="<?php echo $this->HTML->url(array(
"controller" => "users",
"action" => "login")); ?>" method="post">
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在使用它$this->HTML->url()来生成指向我的控制器/操作的链接.在CakePHP 3中,缺少HTMLHelper :: url().文档和迁移指南都没有提到它.还有另一种在Cake 3中生成链接的方法吗?
谢谢!亚历克斯
我想枯干添加一个额外的CSS类或给我的@Html.ValidationSummary()标签提供ID 。我尝试了new与其他HTML Helpers一样的关键字,但是为字符串转换错误提供了匿名类型。这可能吗?如果可以,怎么办?
@Html.ValidationSummary(false, new { @id = "validationSummary" })
Run Code Online (Sandbox Code Playgroud) html-helper ×10
asp.net-mvc ×6
c# ×4
cakephp ×1
cakephp-3.0 ×1
hyperlink ×1
php ×1
routevalues ×1
url-action ×1