2013-06-18 54 views
0

我试图研究一下,但还没有找到适当的解决方案。Razor MVC返回HTML

我试图做的是有一个HTML.Action在页面像这样

Html.Action("Calendar", "GlobalShared") 

里面的行动“日历”,我需要回到传统的日历的HTML的结果控制

public ActionResult Calendar(
        String ID, 
        String CssClass, 
        int CellSpacing, 
        int CellPadding, 
        String BorderWidth, 
        String ShowGridLines, 
        String ShowTitle, 
        String DayNameFormat 
      ) 
     { 
      System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar(); 
      cal.ID = "MyCalendar"; 
      cal.CssClass = "month-view"; 
      cal.CellSpacing = 0; 
      cal.CellPadding = -1; 
      cal.BorderWidth = 0; 
      cal.ShowGridLines = false; 
      cal.ShowTitle = false; 
      cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full; 

     } 

我该怎么做?顺便说一句,我使用HTML.Action是因为我读它返回一个HTML字符串,是否正确或我应该做一些其他方式?

谢谢

编辑。我试图在这个控制器之前,我尝试下面的代码在视图.cshtml和它的作品,但我更喜欢的代码移动到一个控制器

@{ 
        System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar(); 
        cal.ID = "MyCalendar"; 
        cal.CssClass = "month-view"; 
        cal.CellSpacing = 0; 
        cal.CellPadding = -1; 
        cal.BorderWidth = 0; 
        cal.ShowGridLines = false; 
        cal.ShowTitle = false; 
        cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full; 
        cal.RenderControl(new HtmlTextWriter(Html.ViewContext.Writer)); 
       } 

编辑#2。我想在控制器中使用它的原因是,如果将来我想连接一个名为“DayRender”的事件,我可以在控制器中完成。我不能在视图中做同样的事情而不会污染视图页面。

+3

这是不正确的。 'Html.Action'为链接的HTML代码生成一个链接,其URL与控制器中的操作方法相对应。它不用于制作自定义HTML字符串。如果你需要构建HTML,你可以在视图本身中构建它。试图在内存中渲染一些旧的Web控件并输出呈现的HTML将会产生非常复杂和杂乱的代码,并且可能根本不会最终工作。 – David

+0

大卫,我无法找到一个与Thread.CurrentThread.CurrentCulture开箱即用的日历控件,例如我只想使用Calendar控件将正确的html输出到页面。 – Liming

+0

我不认为在MVC中有任何使用Web窗体控件的方式,所以如果您绝对需要使用Web窗体控件,那么您运气不好。你也许可以构建自己的日历。或者Google搜索“ASP.NET MVC日历”会返回(预计)很多结果。 Telerik产品通常特别好。 – David

回答

1

好。多谢你们。我想通了。基本上,我需要使用@ {Html.RenderAction(..)}并在动作本身中使用StringBuilder/StringWriter,然后返回Content(...)。下面

代码在浏览

@{Html.RenderAction("Calendar", "GlobalShared");} 

在控制器

[ChildActionOnly] 
     public ActionResult Calendar(
      ) 
     { 

      System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar(); 
      cal.ID = "MyCalendar"; 
      cal.CssClass = "month-view"; 
      cal.CellSpacing = 0; 
      cal.CellPadding = -1; 
      cal.BorderWidth = 0; 
      cal.ShowGridLines = false; 
      cal.ShowTitle = false; 
      cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full; 
      cal.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(CalendarDayRender); 
      StringBuilder sb = new StringBuilder(); 
      using (System.IO.StringWriter sw = new System.IO.StringWriter(sb)) 
      { 

       System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(sw); 
       cal.RenderControl(writer); 
      } 
      String calHTML = sb.ToString(); 
      return Content(calHTML); 
     } 

     private void CalendarDayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e) 
     { 

      e.Cell.Text = ""; 
      if (e.Day.Date == System.DateTime.Today) 
      { 
       e.Cell.CssClass = "today"; 
       System.Web.UI.HtmlControls.HtmlGenericControl h3 = new System.Web.UI.HtmlControls.HtmlGenericControl("h3"); 
       h3.InnerHtml = HttpContext.GetGlobalResourceObject("JTG_DateTime", "JTK_Today") + " " + e.Day.DayNumberText; 
       e.Cell.Controls.Add(h3); 
      } 

     } 
0

只需将您的代码

@{ 
        System.Web.UI.WebControls.Calendar cal = new System.Web.UI.WebControls.Calendar(); 
        cal.ID = "MyCalendar"; 
        cal.CssClass = "month-view"; 
        cal.CellSpacing = 0; 
        cal.CellPadding = -1; 
        cal.BorderWidth = 0; 
        cal.ShowGridLines = false; 
        cal.ShowTitle = false; 
        cal.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.Full; 
        cal.RenderControl(new HtmlTextWriter(Html.ViewContext.Writer)); 
       } 

在视图中,并从你的日历操作返回查看。

public ActionResult Calendar(
       String ID, 
       String CssClass, 
       int CellSpacing, 
       int CellPadding, 
       String BorderWidth, 
       String ShowGridLines, 
       String ShowTitle, 
       String DayNameFormat 
     ) 
    { 
     return View("Calendar"); 
    } 

您可以创建一个视图模型或使用ViewBag从日历操作这些值传送到新创建的视图

+0

谢谢#UmairP。我想过这个。我想进入控制器的原因是,如果将来我想添加诸如“DayRender”之类的事件,我可以在控制器中执行此操作。通过在局部视图中这样做,我失去了这种能力 – Liming

+0

我不是说你应该做HTML.RenderPartial。您应该使用HTML.RenderAction并从您的应用程序所需的任何位置调用您的操作。 –