2017-06-12 78 views
-1

我想在主页类型视图上预览一个视图。为此,我想调用ListPreviews操作。我想要这个动作来获得给定视图的html正文,然后取第一百个字符左右。从控制器获取视图的HTML - .NET MVC

如何从控制器访问实际的视图html?

+0

能否请你让我知道我可以如何进一步帮助你? – kblau

回答

0

这应该很简单。 在你RouteConfig.cs设置的默认值,矿山看起来像这样:

defaults: new { controller = "Home", action = "Index2006", id = UrlParameter.Optional } 

为控制器/型号:

public class AView 
{ 
    public string theHtml { get; set; } 
} 

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult Index2005(AView AView) 
    { 
     //put breakpoint here to see all the <html> here in view 
     var result = HttpUtility.UrlDecode(AView.theHtml, System.Text.Encoding.Default); 
     return Json(new 
     { 
      Greeting = "Returning data not used" 
     } 
     , @"application/json"); 
    } 

对于您的观点:

<!DOCTYPE html> 
<html id="PassMe"> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index2005</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $(".btn").click(function() { 
       var AView = { theHtml: escape($("#PassMe").html()) }; //JSON.stringify($("#PassMe").html()) 
       $.ajax({ 
        url: '/Home/Index2005', 
        type: 'POST', 
        data: AView, 
        success: function (result) { 
         $("#detail").append(result.Greeting); 
        }, 
        error: function (result) { 
         alert('Error'); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <button style="margin-bottom: 20px;" class="btn btn-default">Click to pass HTML</button> 
</body> 
</html> 
+0

@bchattdeveloper你可以让我知道我可以怎样帮助你吗? – kblau

相关问题