2010-07-13 12 views
6

में प्रोग्रामेटिक रूप से प्रस्तुत करें मैं HTML कोड को एक स्ट्रिंग में उत्पन्न करने के लिए एक HTML कोड प्राप्त करना चाहता हूं, इसे अपने नियंत्रक में संशोधित करें, फिर इसे अपने जेसनआरसल्ट में जोड़ें।एक स्ट्रिंग

मुझे कोड मिला जो मैं आंशिक से बात कर रहा हूं। हालांकि मैं इसे एएसपीएक्स व्यू से करना चाहता हूं।

- अतिरिक्त स्पष्टीकरण:

चलो कहते हैं कि मैं एक पेज Frame.aspx कि/नियंत्रक/फ्रेम

मैं इसे पहले प्रतिक्रिया पर मेरे हाथ पाने के लिए चाहते हैं वापस आ जाएगी के लिए बाहर तो मैं कर सकते हैं करते हैं जेसनपी के साथ लपेटो। मैं हर बार कोड में रिटर्न परिणाम संपादित नहीं करना चाहता, इसलिए मैं प्रोग्राम को प्रोग्रामेटिक रूप से लोड करना चाहता हूं।

/नियंत्रक/फ्रेम वर्तमान में Frame.aspx की सामग्री प्रस्तुत करती है: <html><body>hello</body></html>

के एक समारोह के लिए एक दृश्य रेंडर कर एक स्ट्रिंग बिल्डर

StringBuilder sb = new StringBuilder(); 
RenderView(sb, "Frame"); 

अब sb लेने के लिए और jsonp साथ लपेट में है मान लीजिए:

public JsonResult Frame(string callback) 
{ 
    StringBuilder sb = new StringBuilder(); 
    RenderView(sb, "Frame"); 

    return new JsonResult 
    { 
     Data = "(function() { " + callback + "(" + clientResponse + "); })();" 
     , 
     JsonRequestBehavior = JsonRequestBehavior.AllowGet 
    }; 
} 
+1

संभावित डुप्लिकेट [स्ट्रिंग के रूप में एक दृश्य प्रस्तुत करें] (http://stackoverflow.com/questions/483091/render-a-view-as-a-string) –

+0

कृपया इस प्रश्न को अधिक विस्तार से संपादित करें, और शायद कुछ उदाहरण कोड। उत्तर देने के लिए यहां पर्याप्त जानकारी नहीं है। –

+0

कृपया शीर्षक में "सी #" जैसे टैग शामिल न करें। यह सिर्फ अनावश्यक है। टैग में उन्हें छोड़ना पर्याप्त है। लिंक के लिए –

उत्तर

19

यह एक आकर्षण की तरह काम करता (अतः के माध्यम से यह मिल गया)।

मैं इस तरह इसका इस्तेमाल:

public class OfferController : Controller 
{ 
    [HttpPost] 
    public JsonResult EditForm(int Id) 
    { 
     var model = Mapper.Map<Offer, OfferEditModel>(_repo.GetOffer(Id)); 

     return Json(new { status = "ok", partial = this.RenderPartialViewToString("Edit", model) }); 
    } 
} 



public static partial class ControllerExtensions 
{ 
    public static string RenderPartialViewToString(this ControllerBase controller, string partialPath, object model) 
    { 
     if (string.IsNullOrEmpty(partialPath)) 
      partialPath = controller.ControllerContext.RouteData.GetRequiredString("action"); 

     controller.ViewData.Model = model; 

     using (StringWriter sw = new StringWriter()) 
     { 
      ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialPath); 
      ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw); 
      // copy model state items to the html helper 
      foreach (var item in viewContext.Controller.ViewData.ModelState) 
       if (!viewContext.ViewData.ModelState.Keys.Contains(item.Key)) 
       { 
        viewContext.ViewData.ModelState.Add(item); 
       } 


      viewResult.View.Render(viewContext, sw); 

      return sw.GetStringBuilder().ToString(); 
     } 
    } 
} 
+0

शानदार! धन्यवाद :-) – Abdo

+1

मैं .NET कोर के साथ ऐसा कैसे कर सकता हूं? –

0

माइक हैडलो ने कैप्चरएक्शनएचटीएम() नामक एक फ़ंक्शन के बारे में ब्लॉग किया जो ऐसा करता है। मैंने इसे छोटी, अधिक प्रबंधनीय रिपोर्टों से बड़ी रिपोर्ट लिखने के लिए उपयोग किया है और फिर उन्हें पास कर दिया है।

http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

using System; 
using System.IO; 
using System.Web; 
using System.Web.Mvc; 

namespace Suteki.Common.Extensions 
{ 
    public static class ControllerExtensions 
    { 
     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The controller</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this TController controller, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(controller, null, action); 
     } 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The controller</param> 
     /// <param name="masterPageName">The master page to use for the view</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this TController controller, 
      string masterPageName, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(controller, masterPageName, action); 
     } 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The current controller</param> 
     /// <param name="targetController">The controller which has the action to execute</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this Controller controller, 
      TController targetController, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(targetController, null, action); 
     } 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The current controller</param> 
     /// <param name="targetController">The controller which has the action to execute</param> 
     /// <param name="masterPageName">The name of the master page for the view</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this Controller controller, 
      TController targetController, 
      string masterPageName, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      if (controller == null) 
      { 
       throw new ArgumentNullException("controller"); 
      } 
      if (targetController == null) 
      { 
       throw new ArgumentNullException("targetController"); 
      } 
      if (action == null) 
      { 
       throw new ArgumentNullException("action"); 
      } 

      // pass the current controller context to orderController 
      var controllerContext = controller.ControllerContext; 
      targetController.ControllerContext = controllerContext; 

      // replace the current context with a new context that writes to a string writer 
      var existingContext = System.Web.HttpContext.Current; 
      var writer = new StringWriter(); 
      var response = new HttpResponse(writer); 
      var context = new HttpContext(existingContext.Request, response) {User = existingContext.User}; 
      System.Web.HttpContext.Current = context; 

      // execute the action 
      var viewResult = action(targetController); 

      // change the master page name 
      if (masterPageName != null) 
      { 
       viewResult.MasterName = masterPageName; 
      } 

      // we have to set the controller route value to the name of the controller we want to execute 
      // because the ViewLocator class uses this to find the correct view 
      var oldController = controllerContext.RouteData.Values["controller"]; 
      controllerContext.RouteData.Values["controller"] = typeof(TController).Name.Replace("Controller", ""); 

      // execute the result 
      viewResult.ExecuteResult(controllerContext); 

      // restore the old route data 
      controllerContext.RouteData.Values["controller"] = oldController; 

      // restore the old context 
      System.Web.HttpContext.Current = existingContext; 

      return writer.ToString(); 
     } 
    } 
} 
+0

यह मेरे लिए V3.5 और MVc1.0 के साथ ठीक काम करता है। लेकिन जब मैंने इसे V4.0 और MVC 2.0 में अपग्रेड किया तो संदर्भ शून्य वापस आता है .. कोई विचार ?? – Gokul

0

यहाँ> शुद्ध 4.0 MVC 2.0 में दृश्य पर कब्जा करने और के लिए एक और समाधान नहीं है। मैंने एंड्रयूज मूल सामग्री को कोड की कुछ पंक्तियां जोड़ दी हैं।

public static class ControllerExtensions 
    { 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The controller</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this TController controller, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(controller, null, action); 
     } 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The controller</param> 
     /// <param name="masterPageName">The master page to use for the view</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this TController controller, 
      string masterPageName, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(controller, masterPageName, action); 
     } 

     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The current controller</param> 
     /// <param name="targetController">The controller which has the action to execute</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     public static string CaptureActionHtml<TController>(
      this Controller controller, 
      TController targetController, 
      Func<TController, ViewResult> action) 
      where TController : Controller 
     { 
      return controller.CaptureActionHtml(targetController, null, action); 
     } 




     /// <summary> 
     /// Captures the HTML output by a controller action that returns a ViewResult 
     /// </summary> 
     /// <typeparam name="TController">The type of controller to execute the action on</typeparam> 
     /// <param name="controller">The current controller</param> 
     /// <param name="targetController">The controller which has the action to execute</param> 
     /// <param name="masterPageName">The name of the master page for the view</param> 
     /// <param name="action">The action to execute</param> 
     /// <returns>The HTML output from the view</returns> 
     ///  
    public static string CaptureActionHtml<TController>(this Controller controller, TController targetController, string masterPageName, Func<TController, ViewResult> action) where TController : Controller 

     { 
    if (controller == null) 
    { 
    throw new ArgumentNullException("controller"); 
    } 
    if (targetController == null) 
    { 
    throw new ArgumentNullException("targetController"); 
    } 
    if (action == null) 
    { 
    throw new ArgumentNullException("action"); 
    } 
    // pass the current controller context to orderController 
    var controllerContext = controller.ControllerContext; 
    targetController.ControllerContext = controllerContext; 

    // replace the current context with a new context that writes to a string writer 
    var existingContext = HttpContext.Current; 
    var writer = new StringWriter(); 
    var response = new HttpResponse(writer); 
    var context = new HttpContext(existingContext.Request, response) { User = existingContext.User }; 
    HttpContext.Current = context; 

    // execute the action 
    var viewResult = action(targetController); 

    // change the master page name 
    if (masterPageName != null) 
    { 
    viewResult.MasterName = masterPageName; 
    } 

    // we have to set the controller route value to the name of the controller we want to execute 
    // because the ViewLocator class uses this to find the correct view 
    var oldController = controllerContext.RouteData.Values["controller"]; 
    controllerContext.RouteData.Values["controller"] = typeof(TController).Name.Replace("Controller", ""); 

    // execute the result 
    viewResult.ExecuteResult(controllerContext); 

    StringWriter sw = new StringWriter(); 
    var xx = targetController.TempData["pdf"]; 
    //var viewContext = new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary(targetController.ViewData.Model), new TempDataDictionary(), sw); 
    var viewContext = new ViewContext(controllerContext, viewResult.View, viewResult.ViewData, new TempDataDictionary(), sw); 
    viewResult.View.Render(viewContext, HttpContext.Current.Response.Output); 
    response.Flush(); 

    // restore the old route data 
    controllerContext.RouteData.Values["controller"] = oldController; 

    // restore the old context 
    HttpContext.Current = existingContext; 

    return sw.ToString(); 
    } 



    } 
} 

चीयर्स !!