Asp.net mvc 3 滚动我自己的@Html.BeginfBrm()
我正在编写一个自定义验证集,它将显示div上所有缺失的元素。我希望能够使用一个自定义的Asp.net mvc 3 滚动我自己的@Html.BeginfBrm(),asp.net-mvc-3,extension-methods,html.beginform,Asp.net Mvc 3,Extension Methods,Html.beginform,我正在编写一个自定义验证集,它将显示div上所有缺失的元素。我希望能够使用一个自定义的@Html.BeginForm()方法来写出该div,但我真的不确定从何处开始,因为这比只写出标记或字符串的Html扩展更难破解(表单封装数据/控件,并在末尾由}关闭) 我查看了内置BeginForm()方法的元数据版本,它对我没有多大帮助。本质上,我只想在可能的情况下扩展该方法,并让它写出一个MvcHtmlString的div,它将在JavaScript中显示/隐藏 最终,我正在努力解决的问题是如何编写这个自
@Html.BeginForm()
方法来写出该div,但我真的不确定从何处开始,因为这比只写出标记或字符串的Html扩展更难破解(表单封装数据/控件,并在末尾由}
关闭)
我查看了内置BeginForm()
方法的元数据版本,它对我没有多大帮助。本质上,我只想在可能的情况下扩展该方法,并让它写出一个MvcHtmlString
的div
,它将在JavaScript中显示/隐藏
最终,我正在努力解决的问题是如何编写这个自定义帮助程序,它包含开始和结束组件
@using(Html.BeginForm())
{
...
}
我希望能够做到这样:
@using(Html.VBeginForm())
{
...
}
让它呈现我额外的html
编辑:添加下面建议中的代码
public class VBeginForm : IDisposable
{
private readonly HtmlHelper _helper;
public VBeginForm(HtmlHelper htmlHelper, string areaName)
{
_helper = htmlHelper;
var container = new TagBuilder("form");
container.GenerateId(areaName);
var writer = _helper.ViewContext.Writer;
writer.Write(container.ToString(TagRenderMode.StartTag));
}
public void Dispose()
{
_helper.ViewContext.Writer.Write("</form>");
}
}
公共类VBeginfo:IDisposable
{
私有只读HtmlHelper\u助手;
公共VBeginfo(HtmlHelper HtmlHelper,字符串areaName)
{
_helper=htmlHelper;
var容器=新标记生成器(“表格”);
container.GenerateId(区域名称);
var writer=\u helper.ViewContext.writer;
writer.Write(container.ToString(TagRenderMode.StartTag));
}
公共空间处置()
{
_helper.ViewContext.Writer.Write(“”);
}
}
您需要为打印到helper.ViewContext.Writer的HtmlHelper
类编写一个扩展方法
该方法应返回一个IDisposable
,在其Dispose
方法中打印结束标记。SLaks答案正确,但缺少一些额外信息
如果你想使用传统的(不是不引人注目的)客户端验证,你需要提供一个formContext,并给它一个id,这样客户端验证就可以工作了。在他的解释中,这一部分缺失了
实现这一点的最简单方法是使用返回MvcForm
类的实例,该类创建formContext
,并实现IDisposable
接口
在这个实现中,我需要提供表单的id:
public static MvcForm BeginFormDatosAdicionales(this HtmlHelper htmlHelper,
string id, ..., IDictionary<string, object> htmlAttributes = null)
{
TagBuilder form = new TagBuilder("form");
// attributes
form.MergeAttributes(htmlAttributes);
// action
string formAction = ...;
form.MergeAttribute("action", formAction);
// method
FormMethod method = ...;
form.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
// id
form.MergeAttribute("id", id);
// writes the form's opening tag in the ViewContext.Writer
htmlHelper.ViewContext.Writer.Write(form.ToString(TagRenderMode.StartTag));
// creates an MvcForm (disposable), which creates a FormContext, needed for
// client-side validation. You need to supply and id for it
MvcForm theForm = new MvcForm(htmlHelper.ViewContext);
htmlHelper.ViewContext.FormContext.FormId = form.Attributes["id"];
// The returned object implements IDisposable, and writes the closing form tag
return theForm;
}
在这种情况下,您必须小心为页面中的每个表单创建不同的Id。例如,您可以添加一个整数后缀,该后缀可以存储在HttpContext.Items中,并在每次生成新Id时递增。这可确保在单个页面中生成的所有Id都不同
HttpContext.Current.Items["lastFormId"]
很好,这确实让我朝着正确的方向开始了。我对IDisPossible有点迷茫。我尝试创建我的类,用两种方法扩展IDisPossible,第一种是使用writer写出表单标记,第二种是我的dispose方法写出结束标记。VS2010不喜欢这种方法。你可以这样说您的答案是让我的方法返回IDisposable…然后我将如何让该返回类型调用其dispose()方法?…很抱歉,这里有点混乱。使用
的块将dispose()
您在块末尾放入的对象。对,我明白了,但我如何在方法中写入它应该是渲染的结束表单标记?我只是用我正在尝试但不起作用的内容编辑了我的OP(我将在理顺tagbuilder后清理它)…但这种方法不可行。您需要在一个单独的静态
类中创建一个扩展方法,该类返回IDisposable类的一个实例。(并将HtmlHelper
传递给该实例,以便它可以打印出来)非常好的答案。我抄写了这个代码示例,它完全符合我的需要。
HttpContext.Current.Items["lastFormId"]