Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anton Oosthuizen 192 posts 470 karma points
    Jul 22, 2014 @ 12:43
    Anton Oosthuizen
    0

    ViewData not persisting from Controller to View Umbraco 6.1.6 MVC

    Hi I have a problem where my view data is not persisted. I've read the other threads but I think my problem might be a bit different.

    Firstly I have a template page that renders the child nodes umbraco.library.RenderTemplate

    foreach(var page in dn.Children)
    {
        if(page.NodeTypeAlias!="Page")
        {
       @MvcHtmlString.Create( umbraco.library.RenderTemplate(page.Id));
        }
    }
    

    This in turn calls my form template which call my partial view with a form

    @model FormViewModel
    <div style="padding-top:0px" id="wrapper">
        <!-- PAGE TITLE -->
        <section id="contact" class="container">
         <div class="row">
                <!-- FORM -->
                <div class="col-md-8">
                    <h2>Complete the form below <strong><em></em></strong></h2>
                    @if (ViewData.ContainsKey("SuccessMessage"))
                    {
                        <div class="alert alert-success">
                            <i class="fa fa-check-circle"></i>
                            <strong>@ViewData["SuccessMessage"]</strong>
                        </div>
                        return;
                    }
                    @if (ViewData.ContainsKey("ErrorMessage"))
                    {
                        <div class="alert alert-error">
                            <i class="fa fa-check-circle"></i>
                            <strong>@ViewData["ErrorMessage"]</strong>
                        </div>
    
                    }
                    @using (Html.BeginUmbracoForm("SendEmail", "FormSurface",Model))
                    {
                        <div class="row">
                            <div class="form-group">
                                    <div class="col-md-6">
                                    <label>Course Date *</label>
                                    @Html.TextBoxFor(x => x.CourseDate, new { @type = "date", @class = "form-control", @name = "message", @id = "message" })
                                </div>
                            </div>
                        </div>
                        ...
    

    And My Surface Controller

    public class FormSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
        [HttpPost]
        public ActionResult SendEmail(FormViewModel model)
        {
    
    
    
            //model not valid, do not save, but return current umbraco page
            if (!ModelState.IsValid)
            {
                //Perhaps you might want to add a custom message to the ViewBag
                //which will be available on the View when it renders (since we're not 
                //redirecting)      
                try
                {
                    ViewData["ErrorMessage"] = "Form Error";
                }
                catch
                { }
    
                return CurrentUmbracoPage();
            }
    

    ViewData["ErrorMessage"] is null when I return to CurrentUmbracoPage. Could this be because of my

    @MvcHtmlString.Create( umbraco.library.RenderTemplate(page.Id));
    

    in my temlate ?

  • Andy Butland 373 posts 2057 karma points MVP 4x hq c-trib
    Jul 23, 2014 @ 09:20
    Andy Butland
    0

    Could be.  Could you not just call your partial directly with @Html.Partial?  I'm missing why you need to go via the RenderTemplate method.

    Andy

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies