当使用ASP.NET MVC的Html帮助程序时,我需要将它们包装在Response.Write中,否则它们不会出现.然而我在ASP.NET MVC上找到的样本(例如1和2)似乎没有这样做.某件事情发生了变化,或者我做错了什么?
从样本中我发现它应该是这样的:
<div class="row">
<% Html.ActionLink("View", "Details", "People"); %>
</div>
Run Code Online (Sandbox Code Playgroud)
但是没有显示任何内容,所以我需要将它包装在Response.Write中,如下所示:
<div class="row">
<% Response.Write(Html.ActionLink("View", "Details", "People")); %>
</div>
Run Code Online (Sandbox Code Playgroud) 我的同事非常"热",正确格式化和缩进的html被传递到客户端浏览器.这样,页面源很容易被人阅读.
首先,如果我在我的站点中的许多不同区域中使用了局部视图,那么渲染引擎是否应该为我自动格式化缩进(ala在XmlTextWriter上设置Formatting属性)?
其次,我的同事已经创建了许多用于写入响应的HtmlHelper扩展方法.这些都需要将CurrentIndent参数传递给它们.这对我来说有点不对劲.
有人能帮忙吗?
我问这个问题,提早了业务逻辑和表示逻辑,它让我的思想.我觉得它更容易在代码视图看时,因为我当我看到它是自动的可疑斑可疑的实践.通常它没关系,因为它是表示逻辑,但我总是倾向于仔细观察.
但是当它在HTML帮助器中时,我看起来并不那么接近.其实,我知道,我以前做过,我已经告诉别人的业务逻辑移动到助手.但那是对的吗?
我的猜测是现在,它不是..我想助手的工作是一样的视图的工作.仅限演示.你们有什么感想?
我正在编写一个ASP.NET MVC Html Helper,基本上需要2个HTML Helper返回IHtmlStrings并将它们组合在一起,并将它们作为IHtmlString返回,如下所示:
//this doesn't work
public static IHtmlString CompositeHelper(this HtmlHelper helper, string data)
{
//GetOutput returns an IHtmlString
var output1 = new Component1(data).GetOutput();
var output2 = new Component2(data).GetOutput();
return output1 + output2
}
Run Code Online (Sandbox Code Playgroud)
现在我知道这不会起作用,因为IHtmlString是一个具有复杂类型的实现的接口,但是如果我去的话
return output1.ToHtmlString() + output2.ToHtmlString()
Run Code Online (Sandbox Code Playgroud)
当我从HtmlHelper返回时,我得到一个正常的字符串,它获取HtmlEncoded.
所以我的问题是,如何将输出形式两个IHtmlStrings并将它们组合成一个IHtmlString?
作为概念证明,这里首先是一些控制台输出:
ruby-1.9.2-p180 :010 > x = "<span id='c_3'>s</span>"
=> "<span id='c_3'>s</span>"
ruby-1.9.2-p180 :011 > helper.simple_format(x)
=> "<p><span>s</span></p>"
Run Code Online (Sandbox Code Playgroud)
原因是Rails helper方法在执行的最后simple_format调用该sanitize方法,该方法剥离属性.
我知道这sanitize将允许您指定不应被剥离的属性.我的问题是:是否有可能通过simple_format以某种方式传递"white listed"属性(在本例中为id)?
谢谢!!
我有Html帮助方法,创建一个复选框以及一些文本,我通过.
@Html.CheckBoxFor(x => x.SomeProperty,@<text> <ul> <li> </li> </ul> </text>}))
public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool>> expression, ?? )
{
var chkBox = helper.CheckBoxFor(expression).ToString();
return MvcHtmlString.Create(string.Format("<li>{0}</li><div>{1}</div>", chkBox, ??);
}
Run Code Online (Sandbox Code Playgroud)
那么我的方法的签名是什么呢.一些lambda /表达式或其他东西.
帮助将不胜感激.
问候
成员Parminder
在我的注册中,我有Choose a Question下拉列表:下面的代码
<%= f.select("question_id", Question.all.collect {|p| [ p.body, p.id ] }])) %>
Run Code Online (Sandbox Code Playgroud)
我想得到如下的输出
<select name="question" id="login_fields_question" class="signup_fields" >
<option value="choosequestion" selected="selected" style="font-style:italic;">Choose a question ...</option>
<option value="1"> What is your pet name?</option>
<option value="2">What is the name of your best friend from childhood?</option>
<option value="3">What was the name of your first teacher?</option>
<option value="4">What is the name of your manager at your first job?</option>
<option value="5">What was your first phone number?</option>
<option value="6">What is your vehicle registration …Run Code Online (Sandbox Code Playgroud) 我只是希望文本框编辑器字段显示一个带有ViewBag的值,例如,如果ViewBag.Savings包含'6':
我尝试过类似的东西:
@Html.EditorFor(model => model.Value, ViewBag.Savings)
@Html.ValidationMessageFor(model => model.Value)
Run Code Online (Sandbox Code Playgroud)
我得到编译器错误:' 编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper'没有名为'EditorFor'的适用方法,但似乎有一个名称的扩展方法.无法动态分派扩展方法.考虑转换动态参数或调用扩展方法而不使用扩展方法语法.
什么是正确的语法?
@html.Label()和之间有什么区别@html.LabelFor()?
另外,在Razor上下文中强类型和非强类型视图的情况下,使用这些方法的语法是什么?
如何option label在DropDownListFor上为ListBoxForHTML Helper 添加类似的内容?
我有类似于下面的代码:
<%= Html.ListBoxFor( m => m.SelectedElements, new MultiSelectList(Model.AllPossibleElements, "ID", "Text")) %>
Run Code Online (Sandbox Code Playgroud)
图像波纹管是输出:

我想有一个默认选择(选项标签),如:" 请选择值 ".
html-helper ×10
asp.net-mvc ×8
c# ×2
razor ×2
c#-4.0 ×1
html ×1
indentation ×1
ruby ×1
viewbag ×1
views ×1