Jquery 为什么viewData值未打印成功消息
成功创建用户后,我正在使用视图数据传递消息。我在视图上获取数据,但它没有打印我在视图上获取的值,无法找到问题。请帮忙解决问题。 这是我的密码: 查看:Jquery 为什么viewData值未打印成功消息,jquery,asp.net-mvc,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 ()
@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>
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 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;
}
服务方式:
@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>
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 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;
}
公共字符串AddPre(ConfigMy ConfigMy)
{
字符串messageText=“”;
db.ConfigMys.Add(configMy);
db.SaveChanges();
messageText+=string.Format(“- 航班成功添加”
”;
messageText+=“
”;
返回消息文本;
}
一切正常,除了viewdata值没有在相应的div中打印,但我在viewdata中获得了该值。
这里怎么了?有人能猜出来吗?试试这样的方法,不要使用
@Html.parseHtml
<div class="dummyMessageHead col-md-12 col-sm-12">
ViewData["MessageText"]
</div>
最后一行代码将被处理为已解析的HTML
,而不是文本
Html.parseHtml
不是内置的。如果您已经创建了自己的助手,则需要在此处发布该代码。就目前而言,你的主要问题可能就在于此TempData
,而不是Session
Session
或TempData
中的值设置ViewData
没有意义,因为您可以直接在视图中使用Session
或TempData
这是我的助手课。。公共静态IHtmlString parseHtml(此HtmlHelper帮助程序,字符串HtmlText){返回新的HtmlString(HtmlText);}是的。这是一个完全无用的扩展。只需使用
Html.Raw
。先生,实际上主要的问题是我使用的是jquery ajax,它在成功时创建动态iframe,因此页面不会刷新或重定向到特定的给定位置。。先生,您能分析一下我的代码并确认我如何获得成功信息吗。。我已经更新了我的代码