Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

PartialViewResult in Controller with ViewData #4294

Closed
markusvt opened this issue Mar 15, 2016 · 3 comments
Closed

PartialViewResult in Controller with ViewData #4294

markusvt opened this issue Mar 15, 2016 · 3 comments

Comments

@markusvt
Copy link

Hi. I have the following scenario: In a View I do an ajax request to a controller, which renders a PartialView based on a posted id.

In the controller I can write return PartialView("MyView", model);

Now I just have the problem, that in the PartialView, there is a form which is supposed to have a specific prefix. In a View I can use the following:

@{
   var myViewData = new Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary(ViewData);
   companyViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
}
@Html.Partial("/PathTo/_Partial", Model, myViewData ) 

I think it would be nice, if one could do the same (put a ViewData in the PartialView) in the Controller with that PartialView Result.

@ivaylokenov
Copy link
Contributor

You can do that quite easily. Just create the partial view result by yourself

        public IActionResult Index()
        {
            var myViewData = new Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary(ViewData);
            myViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";

            myViewData.Model = someModel;

            return new PartialViewResult
            {
                ViewData = myViewData,
                ViewName = "MyViewName",
                TempData = TempData
            };
        }

And to simplify it, you can create an extension method:

    public static class ControllerExtensions
    {
        public static PartialViewResult PartialView(this Controller controller, string viewName, object model, ViewDataDictionary viewData)
        {
            if (model != null)
            {
                viewData.Model = model;
            }

            return new PartialViewResult
            {
                ViewName = viewName,
                ViewData = viewData,
                TempData = controller.TempData
            };
        }
    }

And then use it like:

        public IActionResult Index()
        {
            var myViewData = new Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary(ViewData);
            myViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";

            return this.PartialView("myViewName", someModel, myViewData);
        }

@markusvt
Copy link
Author

Ah great! Thank you :)

Edit: Is there also an easy method to serialize the PartialView afterwards into a String. E.g. in my scenario I want to return the generated view plus some additional information in an JSON object

@TaisiaShi
Copy link

Maybe someone will need other variant.

public PartialViewResult MyFunction ()
{
    ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
    return PartialView("ViewName", MyModel);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants