Teerth
Teerth

Reputation: 169

Why viewData value is not printing success message

I am passing a message using view data after successful user creation I am getting the data over view there but its not printing the value what I am getting over there, can not find the issue. Please help with whats wrong. Here is my code:

View:

@model App.ViewModels.ConfigMyViewModel
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#openPopup').click(function () {
        var companyLogo = $('#ConfigMy_CompanyLogo').prop('checked');
        var iFrameWidth = $('#ConfigMy_IFrameWidth').val();
        var iFrameHeight = $('#ConfigMy_IFrameHeight').val();
        var req = { "CompanyLogo": companyLogo,"IFrameWidth": iFrameWidth, "IFrameHeight": iFrameHeight };
        $.ajax({
            type: 'POST',
            url: '@Url.Action("Index", "ConfigMyApp")',
            contentType: "application/json",
            data: JSON.stringify(req),
            datatype: "json",
            success: function (data) {
                var test = '@Url.Action("Index", "Order", new { area = "", UID = ViewData["UIDforUser"] }, this.Request.Url.Scheme)';
                var result = '<iframe width="' + iFrameWidth + '" height="' + iFrameHeight + '" src="' + test + '"></iframe>';
                $("#iframeDiv").show();
                $("#textIframe").val(result);
            },
            error: function () {
                alert("Error: Dynamic content load failed.");
            }
        });       
    });
    });
</script>
@using (Html.BeginForm("Index", "ConfigMyApp", FormMethod.Post, new { @Id = "configpre" }))
{
<div class="row" style="margin:0px 0px 0px 0px;">
        <div class="dummyMessageHead col-md-12 col-sm-12">
            @ViewData["MessageText"]
        </div>
    </div>
<div class="row">
                        <div class="col-lg-3 col-md-3">
                            <div class="btn-group" data-toggle="buttons" id="company">
                                <label class="btn btn-primary btn-sm notActive">
                                    <span class="glyphicon glyphicon-ok"></span>
                                    @Html.RadioButtonFor(m => m.ConfigMy.CompanyLogo, true)
                                </label>
                                <label class="btn btn-primary btn-sm active">
                                    <span class="glyphicon glyphicon-remove"></span>
                                    @Html.RadioButtonFor(m => m.ConfigMy.CompanyLogo, false)
                                </label>
                                &nbsp;Company Logo
                            </div>
                        </div> 
                        <div class="col-lg-4 col-sm-4">
                            IFrame Width 
                            @Html.TextBoxFor(m => m.ConfigMy.IFrameWidth, new { @class = "form-control" })
                        </div>
                        <div class="col-lg-4 col-sm-4">
                            IFrame Height 
                            @Html.TextBoxFor(m => m.ConfigMy.IFrameHeight, new { @class = "form-control" })
                        </div>
            </div>
 <div>
                        <button type="button" id="openPopup" class="btn btn-danger">@Html.CustomText("btnGenerate", "Generate and Copy Code")</button>
                    </div>
                @* Panel Ends *@
                <div class="row" style="display: none" id="iframeDiv">
                    <div class="col-md-11 col-sm-11">
                        <input type="text" id="textIframe" class="form-control" />
                    </div>
                    <div class="col-md-1 col-sm-1">
                        @*<button type="button" id="closeIframeDiv"class="btn btn-primary pull-left"><span class="btn-label"><i class="glyphicon glyphicon-remove"></i></span>close</button>*@
                        <a href="#" id="closeIframeDiv" class="btn btn-primary pull-left"><i class="glyphicon glyphicon-remove"></i></a>
                    </div>
                </div>
}

Controller:

public ActionResult Index()
        {

                ConfigMyViewModel = configMyViewModel = new ConfigMyViewModel();
                if (Session["Message"] != null)
                {
                    ViewData["MessageText"] = Session["Message"].ToString();
                    Session["Message"] = null;
                }
                return View("Index",configMyPreFlightViewModel);
            }
            else
            {
                return RedirectToAction("SignIn", "Account", new { area = "" });
            }
        }

[HttpPost]
        public ActionResult Index(ConfigMy configMy,,bool CompanyLogo,, int IFrameWidth, int IFrameHeight)
        {
                ConfigMyViewModel configMyViewModel = new ConfigMyViewModel();
        bool exists = db.ConfigMys.Any(row => row.UserId == 2);
if (exists != true)
                {
                    if (ModelState.IsValid)
                    {
                        configMy.CompanyLogo = CompanyLogo;
                        configMy.IFrameWidth = IFrameWidth;
                        configMy.IFrameHeight = IFrameHeight;
                        string MessageText = configMyService.AddPre(configMy);
                        Session["Message"] = MessageText;
                    }
                    return RedirectToAction("Index", configMyViewModel);
               }
                else
                {
                    var query = from p in db.ConfigMys
                     where p.UserId == 2
                     select p;
                    foreach (ConfigMy p in query)
                    {
                        p.CompanyLogo = CompanyLogo;
                        p.IFrameWidth = IFrameWidth;
                        p.IFrameHeight = IFrameHeight;
                    }
                    try
                    {
                        string MessageText = "PreFlight Updated Successfully";
                        Session["Message"] = MessageText;
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                    }
                   return RedirectToAction("Index", configMyViewModel);
            }
            else
            {
                return RedirectToAction("SignIn", "Account", new { area = "" });
            }
        }

Method of service:

public string AddPre(ConfigMy configMy)
        {
            string messageText = "<div class=\"row\" style=\"border:1px solid #87CB96; background-color:#CAF9BE; vertical-align:central;\"><p><ul>";
            db.ConfigMys.Add(configMy);
            db.SaveChanges();
            messageText += string.Format("<li>Flight Successfully Added</li>");
            messageText += "</ul></p></div>";
            return messageText;
        }

Everything is working fine except the viewdata value is not printing it in the respecive div but i am getting the value in viewdata . heres the value i am getting What's wrong here? Can anyone figure it out?

Upvotes: 1

Views: 287

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239360

  1. Html.parseHtml is not a built-in. If you've made your own helper, you need to post that code here. As it stands, your main problem may be in that.

  2. If you need to temporarily save a bit of data for the next request, use TempData, not Session.

  3. There's no point in setting ViewData with the value from either Session or TempData as you can use Session or TempData directly in your view.

Upvotes: 1

IndieTech Solutions
IndieTech Solutions

Reputation: 2539

Try something like this , instead of using @Html.parseHtml

<div class="dummyMessageHead col-md-12 col-sm-12">
 ViewData["MessageText"]
</div>

Razr will generate the Html accordingly using the ViewData attribute.

If you need to parse your html you can use:@Html.Raw()

here's an example:

@Html.Raw("<script>alert('Parsed html');</script>")

This last line of code will be processed as parsed HTML and not text

Upvotes: 0

Related Questions