Load view as A string in a Controller - Request a Quote Functionality

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hello Developers/Programmers/Experts,

I am trying to implement request a quote functionality where a customer can send out a quote request. My customer does not want a Wishlist functionality so I planned to tweak around it. I changed all the labels that were already exists for the WishList and I changed it such that it reads with "Quote List", "Quote Request Form", "Send a Quote Request" instead of "Email a Friend". I set up the default email address such that Quote Request is directly sent to the Sales Email address.

Now when the quote request is sent to the store sales team I am trying to get the HTML view and send it as Inline HTML of an email. I copy pasted the Wishlist view, models and controllers and I gave a new name "QuoteRequestEmail", and in that I removed the layout option of the view, and all the editable portion of the view. I removed the additional buttons and fields out of the duplicated view.

After that, when a user clicks on the "Request a Quote" button (originally Email a Friend button) user will be redirected to the form where on click of Send button I load the newly created view (duplicated)  in a string and send it as a HTML body of an email. So basically newly created view just generates a table with all the products selected, and I am trying to send out that products list as an Email's Body HTML.

I have created a route for the new view and when I access the view through the URL it works fine but while loading it up from a WishList "Send Email" controller method it does not work properly. It does not load view's HTML in a String and the basic error of the view is It gets Null Error for the Model of a newly created view.

I created following function which returns an string of a view.
 public string ToHtml(string viewToRender, ViewDataDictionary viewData, ControllerContext controllerContext)
        {
            var result = ViewEngines.Engines.FindView(controllerContext, viewToRender, null);
            

            ViewEngineResult viewResult = System.Web.Mvc.ViewEngines.Engines.FindView(controllerContext, viewToRender, "");
            

            var output = new StringWriter();
            var viewContext = new ViewContext(controllerContext, result.View, viewData, controllerContext.Controller.TempData, output);
            result.View.Render(viewContext, output);
            result.ViewEngine.ReleaseView(controllerContext, result.View);

            return output.ToString();
        }


under the
 public ActionResult EmailWishlistSend(WishlistEmailAFriendModel model, bool captchaValid)


I used the following code to load view on string.

 if (ModelState.IsValid)
            {

                //Prepare the Email body Content
                string _body = Core.Html.HtmlHelper.FormatText(model.PersonalMessage, false, true, false, false, false, false);

                // added by Elisha Cornelius to prepare quote request email.
                // get list of products to be included on the email and send the reuqest to the store owner.

Following is the code portion which calls the method to load view in a string.

                this.ViewData.Model = new Models.ShoppingCart.QuoteRequestEmailModel();
                _body = ToHtml("QuoteRequestEmail", new ViewDataDictionary(new ViewDataDictionary() { { "ShoppingCart", true } }), this.ControllerContext);

                //email
                _workflowMessageService.SendWishlistEmailAFriendMessage(_workContext.CurrentCustomer,
                        _workContext.WorkingLanguage.Id, model.YourEmailAddress, "[email protected]",
                        Core.Html.HtmlHelper.FormatText(model.PersonalMessage, false, true, false, false, false, false));

                model.SuccessfullySent = true;
                model.Result = _localizationService.GetResource("Wishlist.EmailAFriend.SuccessfullySent");

                return View(model);
            }



Can somebody please guide me on this functionality implementation, and where I am going wrong and what exactly needs to be done for this.

Thanks Much,
Elisha Cornelius
11 years ago
Solved.

I was missing the viewdata model was not set. On the action result while the view is access through the routes it does executes the action and sets the viewdata model for the particular view. In the code i posted before I set the model but a null object.

I edited my ToHtml method to make it work properly for this case only.

///<summary>
        ///Function to return view output as String.
        ///Which than can be used to send out as Email Content
        ///</summary>

        public string ToHtml(string viewToRender, ViewDataDictionary viewData, ControllerContext controllerContext, List<ShoppingCartItem> cart)
        {
            var result = ViewEngines.Engines.FindView(controllerContext, viewToRender, null);
            

            ViewEngineResult viewResult = System.Web.Mvc.ViewEngines.Engines.FindView(controllerContext, viewToRender, "");
            

            var output = new StringWriter();
            var viewContext = new ViewContext(controllerContext, result.View, viewData, controllerContext.Controller.TempData, output);
            viewContext.ViewData.Model = PrepareQuoteRequestModel(new QuoteRequestEmailModel(), cart, false);
            result.View.Render(viewContext, output);
            result.ViewEngine.ReleaseView(controllerContext, result.View);

            return output.ToString();
        }


and i set up "viewContext.ViewData.Model = PrepareQuoteRequesteModel(...parametners...);"
This method is basically used when performing the route actions through the HTTP request.

It worked for me. Let me know if someone needs further help with this.

Thanks,
Elisha Cornelius
11 years ago
Hello,

I really need this function. I appreciate if you can email me the detail informaton regarding this function?

[email protected]
11 years ago
Hello,

I really need this function. I appreciate if you can email me the detail informaton regarding this function?

[email protected]
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.