2015-01-12 32 views
6

Cách MVC 6 hiển thị chế độ xem. Phương pháp thực tế trong Razor ViewEngine tạo ra đầu ra html là gì? Ngoài ra nếu có thể, vui lòng giải thích quy trình hiển thị chế độ xem.Asp.net Core hiển thị chế độ xem

Có thể bạn có thể trỏ tôi đến tệp trên nguồn mvc trên github. cảm ơn!

+0

Tôi e rằng điều này hơi quá rộng để trả lời, nhưng về cơ bản một ViewResult ('return View()') biên dịch chế độ xem thích hợp bằng trình biên dịch C# khi đang chạy. Tôi không nghĩ rằng đây là tài liệu bất cứ nơi nào ngoại trừ trong nguồn chính nó. Điều gì khiến bạn không thể tải xuống cây nguồn và tìm thấy những gì bạn đang tìm kiếm? Chính xác thì bạn đang cố gắng làm gì, tại sao bạn muốn biết? – CodeCaster

+0

Tôi đang cố gắng để xây dựng htmlHelper của riêng tôi trên bay qua mã và sau đó xem Render tùy thuộc vào mô hình cung cấp. Tôi cũng đang tìm kiếm mã nguồn nhưng có vẻ hơi khó tìm mà không có tài liệu. – eadam

+0

Mục đích đó nghe có vẻ hơi lạc hậu. Chắc chắn bộ điều khiển có thể/nên đưa ra quyết định. Bạn có thực sự có nghĩa là một phần lượt xem? –

Trả lời

2

Đây là giải pháp hoàn chỉnh về những gì bạn đang tìm kiếm. Tôi sử dụng tiêm phụ thuộc để có được HtmlHelper trong bộ điều khiển. Bạn có thể tiêm helper của riêng bạn nếu bạn muốn.

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(); 
    } 
} 
0

Tôi không biết nếu điều này có thể giúp đỡ, có thể bạn phải bắt đầu nhìn vào những người giúp đỡ tag:

https://github.com/DamianEdwards/TagHelperStarterWeb

họ đang cố gắng một cách khác nhau để tạo ra những người giúp đỡ tích hợp trong trang một cách tự nhiên hơn.

Các vấn đề liên quan