1

I have a partial view named Form.cshtml in Shared folder. There is not any action and controller for it. It is in Shared folder and I just render it in a view named Index in Home controller.

All of codes I found needed a controller context but I do not have any controllers. So can I get the Html by the view physical address or any other way?

Hamid Reza
  • 2,913
  • 9
  • 49
  • 76

2 Answers2

4

In Controller You must do like this

   public ActionResult GetPartial()
    {
      var viewStr=RenderRazorViewToString("~/Views/Home/Partial1.cshtml",new object())
      return content(viewStr);
    }

    // by this method you can  get string of view -- Update
    public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                                         viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                             ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
Stepan Tripal
  • 651
  • 6
  • 7
M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • This will return a partial view.I need a method in the controller that return a string containing the html of a partial view inside shared folder. – Hamid Reza Oct 11 '14 at 06:20
  • http://stackoverflow.com/a/2759898/4104866 you can use this to render any partial to string,a i think any controller context will be work ,because of using full path to view – Kostia Mololkin Oct 11 '14 at 06:22
  • 1
    @HamidReza if you have an ajax request and your content type is html this code return you partial view html – M.Azad Oct 11 '14 at 06:23
  • @M.Azad I am seeing this error: Type 'ASP._Page_Views_Shared_Form_cshtml' does not inherit from 'System.Web.UI.UserControl'. – Hamid Reza Oct 11 '14 at 06:58
  • @HamidReza you call your action with ajax request? – M.Azad Oct 11 '14 at 07:17
  • No. After posting a form I want to get html content of the submitted form(that is placed in a partial view) in the controller. – Hamid Reza Oct 11 '14 at 07:27
  • @HamidReza I Update my answer and its worked for me...chek it – M.Azad Oct 11 '14 at 07:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62870/discussion-between-hamid-reza-and-m-azad). – Hamid Reza Oct 11 '14 at 08:29
3

all you need to specify full path to view

Home/Index.cshtml

@Html.Partial("/Views/Shared/Form.cshtml")

Edit

https://stackoverflow.com/a/2759898/4104866 you can use this to render any partial to string,a i think any controller context will be work ,because of using full path to view

Community
  • 1
  • 1
Kostia Mololkin
  • 868
  • 9
  • 25
  • I update the question title. I want to get the html it in the controller. – Hamid Reza Oct 11 '14 at 06:16
  • In Controller Must add ~ in View address "~/Views/..." – M.Azad Oct 11 '14 at 06:17
  • Kostina I tried three type of callings: RenderRazorViewToString("~/Views/Shared/Form.cshtml")=>SearchedLocations is null. RenderRazorViewToString("Form")=>SearchedLocation is null. RenderRazorViewToString("Form.cshtml")=>SearchedLocations(~/Views/Shared/Form.cshtml.cshtml,~/Views/Shared/Form.cshtml.vbhtml) – Hamid Reza Oct 11 '14 at 07:00
  • RenderRazorViewToString("/Views/Shared/Form.cshtml") did you try this? without `~` – Kostia Mololkin Oct 11 '14 at 07:20
  • @KostiaMololkin SearchedLocation is null again and returning string is "". – Hamid Reza Oct 11 '14 at 07:49
  • 1
    this.RenderPartialViewToString("//Views//Shared//Form.cshtml", null) trying this and its work for me – Kostia Mololkin Oct 11 '14 at 08:10