2015-05-18 69 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