2015-05-18 40 views
1

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

查看:

@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值不打印在respecive div,但我得到的viewdata值。 heres the value i am getting 这里有什么问题?任何人都可以弄明白吗?

回答

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,而不是文本的代码最后一行将被处理

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

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

  3. 有没有在设置ViewData同来自SessionTempData值,你可以SessionTempData直接在您的视图中使用是没有意义的。

+0

这里是我的助手类.. 公共静态IHtmlString parseHtml(此的HtmlHelper助手,字符串的htmlText) { 返回新HtmlString(的htmlText); } – Teerth

+2

是的。这是完全无用的扩展。只需使用'Html.Raw'。 –

+0

先生其实主要的问题是我使用的是jquery ajax,并且它在成功时创建了动态iframe,因此页面不会刷新或不会重定向到特定的位置。先生,请您分析我的代码并确认如何才能我得到了成功的消息..我已经更新了我的代码 – Teerth