为什么viewData值不是打印成功消息

时间:2015-05-18 16:19:53

标签: jquery asp.net-mvc

我在成功创建用户后使用视图数据传递消息我在那里获取数据但是它没有打印我在那里得到的值,找不到问题。请帮忙解决什么问题。 这是我的代码:

查看:

@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>
}

控制器:

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 = "" });
            }
        }

服务方式:

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;
        }

一切正常,除了viewdata值没有在相应的div中打印,但我在viewdata中得到了值。 heres the value i am getting 这有什么不对?有人能想出来吗?

2 个答案:

答案 0 :(得分:1)

  1. Html.parseHtml不是内置的。如果您已经创建了自己的帮助程序,则需要在此处发布该代码。就目前而言,你的主要问题可能在于。

  2. 如果您需要为下一个请求临时保存一些数据,请使用TempData,而不是Session

  3. 使用ViewDataSession中的值设置TempData是没有意义的,因为您可以使用Session或{{1}直接在你的视图中。

答案 1 :(得分:0)

尝试这样的事情,而不是使用@Html.parseHtml

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

Razr会使用Html属性相应地生成ViewData

如果您需要解析html,可以使用:@Html.Raw()

这是一个例子:

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

最后一行代码将被解析为已解析HTML而不是文本