Asp.net 如何在表单元素中呈现输入元素
Antaris RazorEngine v3.9.3在ASP.NET MVC4控制器中用于创建表单元素。 结果html输入元素在表单元素外部呈现。 比如说Asp.net 如何在表单元素中呈现输入元素,asp.net,asp.net-mvc,asp.net-mvc-4,razor,razorengine,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,Razorengine,Antaris RazorEngine v3.9.3在ASP.NET MVC4控制器中用于创建表单元素。 结果html输入元素在表单元素外部呈现。 比如说 @using (Html.BeginForm("Test", "Upload")) { <input type="file" /> <input type="submit"/> } 型号: public IHtmlString RenderScript() { var config = new Templat
@using (Html.BeginForm("Test", "Upload"))
{
<input type="file" />
<input type="submit"/>
}
型号:
public IHtmlString RenderScript() {
var config = new TemplateServiceConfiguration();
config.BaseTemplateType = typeof(HtmlTemplateBase<>);
config.Namespaces = new HashSet<string>(new string[] {
"System",
"System.Collections",
"System.Collections.Generic",
"System.Globalization",
"System.Linq",
"System.Web",
"System.Web.Mvc",
"System.Web.Mvc.Html"
});
razor = RazorEngineService.Create(config);
// In real application colModel is retrieved from database
var colModel=@"
@using (Html.BeginForm(""Test"", ""Upload""))
{
<input type='file' />
<input type='submit'/>
}
";
string res = razor.RunCompile(colmodel, colmodel, typeof(object), new object());
return MvcHtmlString.Create(res);
}
public IHtmlString RenderScript(){
var config=new TemplateServiceConfiguration();
config.BaseTemplateType=typeof(HtmlTemplateBase);
config.Namespaces=newhashset(新字符串[]){
“系统”,
“系统.集合”,
“System.Collections.Generic”,
“体制.全球化”,
“System.Linq”,
“System.Web”,
“System.Web.Mvc”,
“System.Web.Mvc.Html”
});
razor=RazorEngineService.Create(配置);
//在实际应用中,模型是从数据库中检索的
var colModel=@”
@使用(Html.BeginForm(“测试”、“上传”))
{
}
";
string res=razor.RunCompile(colmodel,colmodel,typeof(object),new object());
返回MvcHtmlString.Create(res);
}
模板基类:
using RazorEngine.Templating;
using RazorEngine.Text;
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
public class HtmlTemplateBase<T> : TemplateBase<T>, IViewDataContainer
{
// http://stackoverflow.com/questions/8561164/razorengine-issues-with-html
HtmlHelper<T> helper = null;
ViewDataDictionary viewdata = null;
public HtmlHelper<T> Html
{
get
{
// https://github.com/Antaris/RazorEngine/issues/150
if (helper == null)
{
var p = WebPageContext.Current;
var wvp = p.Page as WebViewPage;
var context = wvp != null ? wvp.ViewContext : null;
context.Writer = this.CurrentWriter;
helper = new HtmlHelper<T>(context, this);
}
return helper;
}
}
public ViewDataDictionary ViewData
{
get
{
if (viewdata == null)
{
viewdata = new ViewDataDictionary();
viewdata.TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = string.Empty };
if (this.Model != null)
{
viewdata.Model = Model;
}
}
return viewdata;
}
set
{
viewdata = value;
}
}
public override void WriteTo(TextWriter writer, object value)
{
if (writer == null)
throw new ArgumentNullException("writer");
if (value == null) return;
//try to cast to RazorEngine IEncodedString
var encodedString = value as IEncodedString;
if (encodedString != null)
{
writer.Write(encodedString);
}
else
{
var htmlString = value as IHtmlString;
if (htmlString != null) writer.Write(htmlString.ToHtmlString());
else
{
encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value);
writer.Write(encodedString);
}
}
}
}
使用RazorEngine.Templating;
使用RazorEngine.Text;
使用制度;
使用System.IO;
使用System.Web;
使用System.Web.Mvc;
使用System.Web.Web网页;
公共类HtmlTemplateBase:TemplateBase,IViewDataContainer
{
// http://stackoverflow.com/questions/8561164/razorengine-issues-with-html
HtmlHelper helper=null;
ViewDataDictionary viewdata=null;
公共Html帮助器Html
{
得到
{
// https://github.com/Antaris/RazorEngine/issues/150
if(helper==null)
{
var p=WebPageContext.Current;
var wvp=作为WebViewPage的页面;
var context=wvp!=null?wvp.ViewContext:null;
context.Writer=this.CurrentWriter;
helper=newhtmlhelper(上下文,this);
}
返回助手;
}
}
公共视图数据字典视图数据
{
得到
{
如果(viewdata==null)
{
viewdata=新的ViewDataDictionary();
viewdata.TemplateInfo=newtemplateinfo(){HtmlFieldPrefix=string.Empty};
如果(this.Model!=null)
{
viewdata.Model=Model;
}
}
返回视图数据;
}
设置
{
viewdata=值;
}
}
public override void WriteTo(TextWriter,对象值)
{
if(writer==null)
抛出新的异常(“编写器”);
if(value==null)返回;
//试着投给拉佐林·伊诺德斯特林
var encodedString=作为encodedString的值;
if(encodedString!=null)
{
writer.Write(编码字符串);
}
其他的
{
var htmlString=作为IHtmlString的值;
if(htmlString!=null)writer.Write(htmlString.ToHtmlString());
其他的
{
encodedString=TemplateService.EncodedStringFactory.CreateEncodedString(值);
writer.Write(编码字符串);
}
}
}
}
也发布在中,尝试为表单中的输入控件命名。我添加了name属性,但问题仍然存在。请注意,这个问题纯粹是关于视图生成输出顺序,而不是关于输入元素。除了输入元素之外,还可以有任何其他文本,问题仍然存在。确定。我去年确实遇到过这个问题,我不确定我是如何解决的。你能试着找到解决办法吗。我试图覆盖其他WriteTo方法,但看起来他们都调用了此WriteTo,BeginForm没有调用WriteTo,而是直接写入视图只是猜测一下,尝试从/Bin文件夹中删除所有内容,重新编译并再次运行(虽然没有任何意义,但尝试一下!)!!
@Model.RenderScript()
public IHtmlString RenderScript() {
var config = new TemplateServiceConfiguration();
config.BaseTemplateType = typeof(HtmlTemplateBase<>);
config.Namespaces = new HashSet<string>(new string[] {
"System",
"System.Collections",
"System.Collections.Generic",
"System.Globalization",
"System.Linq",
"System.Web",
"System.Web.Mvc",
"System.Web.Mvc.Html"
});
razor = RazorEngineService.Create(config);
// In real application colModel is retrieved from database
var colModel=@"
@using (Html.BeginForm(""Test"", ""Upload""))
{
<input type='file' />
<input type='submit'/>
}
";
string res = razor.RunCompile(colmodel, colmodel, typeof(object), new object());
return MvcHtmlString.Create(res);
}
using RazorEngine.Templating;
using RazorEngine.Text;
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
public class HtmlTemplateBase<T> : TemplateBase<T>, IViewDataContainer
{
// http://stackoverflow.com/questions/8561164/razorengine-issues-with-html
HtmlHelper<T> helper = null;
ViewDataDictionary viewdata = null;
public HtmlHelper<T> Html
{
get
{
// https://github.com/Antaris/RazorEngine/issues/150
if (helper == null)
{
var p = WebPageContext.Current;
var wvp = p.Page as WebViewPage;
var context = wvp != null ? wvp.ViewContext : null;
context.Writer = this.CurrentWriter;
helper = new HtmlHelper<T>(context, this);
}
return helper;
}
}
public ViewDataDictionary ViewData
{
get
{
if (viewdata == null)
{
viewdata = new ViewDataDictionary();
viewdata.TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = string.Empty };
if (this.Model != null)
{
viewdata.Model = Model;
}
}
return viewdata;
}
set
{
viewdata = value;
}
}
public override void WriteTo(TextWriter writer, object value)
{
if (writer == null)
throw new ArgumentNullException("writer");
if (value == null) return;
//try to cast to RazorEngine IEncodedString
var encodedString = value as IEncodedString;
if (encodedString != null)
{
writer.Write(encodedString);
}
else
{
var htmlString = value as IHtmlString;
if (htmlString != null) writer.Write(htmlString.ToHtmlString());
else
{
encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value);
writer.Write(encodedString);
}
}
}
}