也许你可以指点我在github上的mvc源文件.谢谢!
解决方法 这是您正在寻找的完整解决方案.我使用依赖注入来获取控制器中的 HtmlHelper.如果你愿意的话,你可以注入自己的助手.using Microsoft.AspNet.HTML.Abstractions;using Microsoft.AspNet.Mvc;using Microsoft.AspNet.Mvc.ModelBinding;using Microsoft.AspNet.Mvc.Rendering;using Microsoft.AspNet.Mvc.VIEwEngines;using Microsoft.AspNet.Mvc.VIEwFeatures;using Microsoft.AspNet.Mvc.VIEwFeatures.Internal;using Microsoft.Extensions.WebEncoders;using System.ComponentModel.DataAnnotations;using System; public class MyController : Controller { private Readonly IHTMLGenerator HTMLGenerator; ICompositeVIEwEngine vIEwEngine; IModelMetadataProvIDer MetadataProvIDer; private Readonly IHTMLHelper helper; IHTMLEncoder HTMLEncoder; IUrlEncoder urlEncoder; IJavaScriptStringEncoder JavaScriptStringEncoder; public MyController(IHTMLHelper helper,IHTMLGenerator HTMLGenerator,ICompositeVIEwEngine vIEwEngine,IModelMetadataProvIDer MetadataProvIDer,IHTMLEncoder HTMLEncoder,IUrlEncoder urlEncoder,IJavaScriptStringEncoder JavaScriptStringEncoder) { this.HTMLGenerator = HTMLGenerator; this.vIEwEngine = vIEwEngine; this.MetadataProvIDer = MetadataProvIDer; this.HTMLEncoder = HTMLEncoder; this.urlEncoder = urlEncoder; this.JavaScriptStringEncoder = JavaScriptStringEncoder; this.helper = helper; } [httpGet] public IActionResult MyHTMLGenerator() { Myviewmodel temp = new Myviewmodel(); var options = new HTMLHelperOptions(); options.ClIEntValIDationEnabled = true; VIEwDataDictionary<Myviewmodel> dic = new VIEwDataDictionary<Myviewmodel>(this.MetadataProvIDer,new ModelStateDictionary()); VIEwContext cc = new VIEwContext(ActionContext,new FakeVIEw(),dic,TempData,TextWriter.Null,options); var type = typeof(Myviewmodel); var Metadata = this.MetadataProvIDer.GetMetadataForType(type); ModelExplorer modelEx = new ModelExplorer(this.MetadataProvIDer,Metadata,temp); VIEwData["Description"] = "test desc"; VIEwData["ID"] = 1; this.VIEwData = new VIEwDataDictionary(this.MetadataProvIDer,new ModelStateDictionary()); IHTMLHelper<Myviewmodel> dd = new HTMLHelper<Myviewmodel>(this.HTMLGenerator,this.vIEwEngine,this.MetadataProvIDer,this.HTMLEncoder,this.urlEncoder,this.JavaScriptStringEncoder); ((ICanHasVIEwContext)dd).Contextualize(cc); dd.VIEwContext.VIEwData = this.VIEwData; var desc = GetString(dd.TextBoxFor(m => m.ID)); var ID = GetString(dd.TextBoxFor(m => m.Description)); // Do whatever you want with the ID and desc return new ContentResult() { Content = ID + desc }; } public static string GetString(IHTMLContent content) { var writer = new System.IO.StringWriter(); content.Writeto(writer,new HTMLEncoder()); return writer.ToString(); }}public class Myviewmodel : BaseAssetviewmodel{ // [RegularExpression(@"^-?\d{1,13}(\.\d{0,5})?$|^-?\.\d{1,5}$")] [required] public int ID { get; set; } [MinLength(2)] public string Description { get; set; } // Property with no valIDation public string Other { get; set; }}public class FakeVIEw : IVIEw{ string IVIEw.Path { get { throw new NotImplementedException(); } } public Task RenderAsync(VIEwContext vIEwContext) { throw new InvalIDOperationException(); } Task IVIEw.RenderAsync(VIEwContext context) { throw new NotImplementedException(); }}总结
以上是内存溢出为你收集整理的剃刀 – Asp.net Core如何呈现视图全部内容,希望文章能够帮你解决剃刀 – Asp.net Core如何呈现视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)