在我的研究中,我看到很多很多關於這個問題的問題,但他們中的任何一個都沒有幫助我。IE中的JSON安全警告,嘗試在MVC3應用程序中下載
我已經嘗試了每個答案的解決方案,但他們都沒有工作。
因此,我會逐一展示代碼,以及我嘗試過的方法以及在哪裏。
首先,我的看法..由2種形式的局部視圖,主視圖點擊這裏:
@using MyApplication.Select.Web.Helpers;
@model SearchUsersViewModel
@*Scripts*@
<script src="@Url.Content("~/Scripts/SearchEntity.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ManageUsers.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/FormValidation.js")" type="text/javascript"></script>
<script type="text/javascript">
var theme = "admin"; //(lender, school, admin, or public)
var cssArray = ["ManageUsers.css", "ActionIcons.css"];
//Invoke loadCSS Method from CSSLoader.js
loadCSS(theme, cssArray);
//Set active tab from NavigationAdmin.js
setActiveNavTab("tab01"); //(tab01, tab02, tab03, tab04, tab05, tab06)
document.getElementById("AdminPageHeaderIMG").className = "HeaderImageManageUsers";
</script>
<div>
<table id="" cellpadding="0" cellspacing="0" border="0">
<tr style="height: 60px;">
<td>
<div class="PageHeaderDescriptionDiv">
Welcome to My Application
</div>
</td>
</tr>
<tr style="height: 1px;">
<td class="ContentDividerHoriz"></td>
</tr>
</table>
<table class="SearchUsersMainTable" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>
<div class="SearchByUserDataTable">
@using (Html.BeginForm()) {
@Html.HiddenFor(model => model.SearchFilterType)
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 30px;">
<td class="Header01">
User Search
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 20px;">
<td class="Header02">
Search By User Information
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 1px;">
<td class="ContentDividerHoriz_425"></td>
</tr>
</table>
<table id="searchByUserDataTable" cellpadding="0" cellspacing="0" border="0">
<tr style="height: 26px;">
<td class="leftColumn">
@Html.LabelFor(model => model.LastName)
</td>
<td class="rightColumn">
@Html.TextBoxFor(model => model.LastName, new { @class = "TextField_220" })
</td>
</tr>
<tr style="height: 26px;">
<td class="leftColumn">
@Html.LabelFor(model => model.Username)
</td>
<td class="rightColumn">
@Html.TextBoxFor(model => model.Username, new { @class = "TextField_220" })
</td>
</tr>
<tr style="height: 26px;">
<td class="leftColumn">
@Html.LabelFor(model => model.EmailAddress)
</td>
<td class="rightColumn">
@Html.TextBoxFor(model => model.EmailAddress, new { @class = "TextField_220" })
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td id="filterByUserError" style="width: 300px; color: #ff0000;"></td>
<td align="right" style="width: 50px;">
<div>
<input id="filterByUserButton" type="submit" value="Search" />
</div>
</td>
<td style="width: 75px;"></td>
</tr>
</table>
}
</div>
</td>
<td style="width: 20px;"></td>
<td>
<div class="SearchByEntityDataTable">
@using (Html.BeginForm()) {
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 28px;">
<td style="width: 425px;"></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 20px;">
<td class="Header02">
Search By Entity Information
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 1px;">
<td class="ContentDividerHoriz_425"></td>
</tr>
</table>
<table id="searchByEntityDataTable" cellpadding="0" cellspacing="0" border="0">
<tr style="height: 26px;">
<td class="leftColumn">
@Html.LabelFor(model => model.EntityTypeID)
</td>
<td class="rightColumn">
@Html.DropDownListFor(model => model.EntityTypeID, new SelectList(Model.EntityTypes, "ID", "Name"), new { id = "entityTypeDropDown", @class = "DropDown_220" })
</td>
</tr>
<tr style="height: 26px;">
<td class="leftColumn">
@Html.LabelFor(model => model.SearchField, new { id = "entityTypeSearchLabel"})
</td>
<td class="rightColumn">
@Html.TextBoxFor(model => model.SearchField, new { id = "entityTypeSearchField", @class = "ui-widget TextField_220" })
</td>
</tr>
<tr style="height: 26px;">
<td class="leftColumn"></td>
<td class="rightColumn"></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td id="filterByEntityError" style="width: 300px; color: #ff0000;"></td>
<td align="right" style="width: 50px;">
<div>
<input id="filterByEntityButton" type="submit" value="Search" />
</div>
</td>
<td style="width: 75px;"></td>
</tr>
</table>
}
</div>
</td>
</tr>
</tbody>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 1px;">
<td class="ContentDividerHoriz"></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 33px;">
<td style="width: 870px;">
<div class="TelerikGridHeaderBkgd">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="Header01" style="width: 150px; padding: 0px 0px 4px 5px;">
User Search Results
</td>
<td style="width: 10px;"></td>
<td style="width: 710px;">
<table cellpadding="0" cellspacing="0" border="0">
<tr style="height: 4px;">
<td style="width: 710px;"></td>
</tr>
</table>
<table>
<tr style="height: 20px;">
<td style="width: 188px;">
@*Resend Invitation*@
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ActionIcon_ResendInvitationOn"></td>
<td style="padding-left: 5px; padding-right: 10px;">
<span class="SearchUsersLegendText">= Resend Invitation</span>
</td>
</tr>
</table>
</td>
<td style="width: 140px;">
@*Account Approved Status*@
<table width="140" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ActionIcon_AccountStatusLegend"></td>
<td style="padding-left: 5px; padding-right: 10px;">
<div class="SearchUsersLegendText">
<span>= </span>
<span style="color: #839f1b;">Active</span>
<span>/</span>
<span style="color: #d6161f;">Inactive</span>
</div>
</td>
</tr>
</table>
</td>
<td style="width: 162px;">
@*Account Lock Status*@
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ActionIcon_UnlockAccountOn"></td>
<td style="padding-left: 5px; padding-right: 10px;">
<span class="SearchUsersLegendText">= Unlock Account</span>
</td>
</tr>
</table>
</td>
<td style="width: 170px;">
@*Reset Password*@
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ActionIcon_ResetPasswordOn"></td>
<td style="padding-left: 5px; padding-right: 10px;">
<span class="SearchUsersLegendText">= Reset Password</span>
</td>
</tr>
</table>
</td>
<td style="width: 145px;">
@*Edit Account*@
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ActionIcon_EditOn"></td>
<td style="padding-left: 5px; padding-right: 10px;">
<span class="SearchUsersLegendText">= Edit Account</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr style="height: 6px;">
<td style="width: 710px;"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td style="width: 870px;">
<div id="searchResults">
@Html.Partial("SearchResultsPartial", Model)
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
SearchResultsPartial是在這裏:
@model SearchUsersViewModel
@*Scripts*@
<link href="@Url.Content("~/Content/styles/TelerikCustom.css")" rel="stylesheet" type="text/css" />
@(Html.Telerik().Grid(Model.Users)
.Name("Users").TableHtmlAttributes(new { style = "width: 870px;"})
.Columns(columns => {
columns.Bound(o => o.EntityTypeName).Title("Entity Type");
columns.Bound(o => o.FirstName).Title("First Name");
columns.Bound(o => o.LastName).Title("Last Name");
columns.Bound(o => o.Username).Title("Username");
columns.Template(
@<text>
<a href="mailto:@item.EmailAddress" target="blank">@item.EmailAddress</a>
</text>).Title("Email").HtmlAttributes(new { style = "text-align: center" }).HeaderHtmlAttributes(new { style = "text-align: center" });
columns.Template(
@<text>
@{ if (@item.MembershipID == 0) {
<div class="ActionIcon_ResendInvitationOn" title="Resend Invitation" onclick="resendInvitation(@item.EntityID, @item.EntityTypeID, '@item.EmailAddress')"></div>
}
else {
if ((bool) item.IsApproved) {
<div class="ActionIcon_AccountStatusOn" title="Disable Account" onclick="setApprovalStatus('@item.Username', false)"></div>
}
else {
<div class="ActionIcon_AccountStatusOff" title="Enable Account" onclick="setApprovalStatus('@item.Username', true)"></div>
}
if ((bool) item.IsLockedOut) {
<div class="ActionIcon_UnlockAccountOn" title="Unlock Account" onclick="unlockAccount('@item.Username')"></div>
}
else {
<div class="ActionIcon_ResetPasswordOn" title="Reset Password" onclick="resetPassword('@item.Username')"></div>
}
<div class="ActionIcon_EditOn" title="Edit User" onclick="location.href='@Url.Action("Edit", "Admin", new { id = item.MembershipID, username = item.Username })'"></div>
}
}
</text>).Title("Actions");
columns.Bound(o => o.RowNumber).Hidden(true);
columns.Bound(o => o.MembershipID).Hidden(true);
columns.Bound(o => o.EntityID).Hidden(true);
columns.Bound(o => o.EntityTypeID).Hidden(true);
})
)
<div>
Total Rows:
@{
if ([email protected]()) {
@Html.Label("0")
}
else {
@Model.Users.First().TotalRows
}
}
</div>
兩種形式可以做搜索,它是互斥搜索。按用戶數據搜索或按實體數據搜索,但不能同時搜索。每個火源關閉自己的JavaScript它運行Ajax調用的提交按鈕:
function filterByUserSearch() {
var lastName = document.getElementById("LastName");
var username = document.getElementById("Username");
var emailAddress = document.getElementById("EmailAddress");
var entityTypeID = document.getElementById("entityTypeDropDown");
var entityName = document.getElementById("entityTypeSearchField");
var searchFilterType = document.getElementById("SearchFilterType");
//alert("User Search");
entityTypeID.value = 0;
entityName.value = "";
searchFilterType.value = 0;
$.ajax({
url: "/Admin/Search/",
dataType: "json",
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: { LastName: lastName.value, Username: username.value, EmailAddress: emailAddress.value, SearchFilterType: searchFilterType.value },
success: function (result) {
$('#resultSpan').html('');
if(result.Success) {
$('#searchResults').html(result.Data);
if (result.ResultMessage != '<li></li>') {
$('#resultSpan').append($("<ul id='successMsg' style='list-style: none;' />").append(result.ResultMessage)).addClass("AjaxSuccessText");
$('#successMsg').css("padding-left", "0");
showResultPopUpDiv("ajaxResultWrapperDiv", "Action was Successful!");
}
}
else {
$('#resultSpan').append($("<ul id='errorMsg' style='list-style: none;' />").append(result.ResultMessage)).addClass("AjaxErrorText");
$('#errorMsg').css("padding-left", "0");
showResultPopUpDiv("ajaxResultWrapperDiv", "Ooops! There was an Error");
}
}
});
return false;
}
被調用控制器動作是在這裏:
[HttpPost]
public JsonResult Search(SearchUsersViewModel model) {
try {
if (model.SearchFilterType == SearchFilterType.ByUserData)
return SearchForUsersByUserData(model, string.Empty);
if (model.SearchFilterType == SearchFilterType.ByEntityData)
return SearchForUsersByEntityData(model, string.Empty);
}
catch (Exception ex) {
ModelState.AddModelError("", ModelStateErrorUtility.WrapResultMessageForList(ex.Message));
}
return Json(new { Success = false, ResultMessage = ModelStateErrorUtility.GetModelStateErrors(ModelState) }, "application/json", JsonRequestBehavior.AllowGet);
}
private JsonResult SearchForUsersByUserData(SearchUsersViewModel model, string resultMessage) {
if (model.LastName != null || model.Username != null || model.EmailAddress != null) {
var listOfMatchingUsers = SearchUserService.SearchByUserData(model.LastName, model.Username, model.EmailAddress);
return PrepareSearchResultsForPartialView(model, listOfMatchingUsers, resultMessage);
}
throw new ArgumentNullException("Last Name, Username or Email Address must be entered for search");
}
private JsonResult SearchForUsersByEntityData(SearchUsersViewModel model, string resultMessage) {
if ((model.EntityTypeID == 1) || (model.EntityTypeID > 0 && model.SearchField != null)) {
var listOfMatchingUsers = SearchUserService.SearchByEntityData(model.EntityTypeID, model.SearchField);
return PrepareSearchResultsForPartialView(model, listOfMatchingUsers, resultMessage);
}
throw new ArgumentNullException("Entity Type must be entered for search");
}
private JsonResult PrepareSearchResultsForPartialView(SearchUsersViewModel model, ICollection<SearchUserResultsDTO> list, string resultMessage) {
return Json(new { Success = true, ResultMessage = ModelStateErrorUtility.WrapResultMessageForList(resultMessage), Data = RenderRazorViewToString("SearchResultsPartial", PrepareSearchResultsForModel(list, model)) }, "application/json", JsonRequestBehavior.AllowGet);
}
private static SearchUsersViewModel PrepareSearchResultsForModel(ICollection<SearchUserResultsDTO> listOfMatchingUsers, SearchUsersViewModel model) {
if (listOfMatchingUsers.Count != 0) {
model.Users = listOfMatchingUsers.Select(item => new UserEditViewModel(item)).ToList();
}
return model;
}
private string RenderRazorViewToString(string viewName, object model) {
ViewData.Model = model;
using (var sw = new StringWriter()) {
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
我已經試過:
返回行動方法設置爲JsonResult
沒有指定響應內容類型,ajax調用contentType:application/json;字符集= UTF-8
- IE 8指向登臺服務器IIS提示
- IE 9指向下載文件到臨時服務器IIS在所有
- Chrome並沒有指向臨時服務器IIS什麼事情都不做所有
- 瀏覽器指向本地網絡服務器做什麼都沒有
應用/ JSON的;字符集= UTF-8響應規定,對AJAX調用任何內容類型
- IE 8指向登臺服務器IIS提示
- IE 9指向下載文件到臨時服務器IIS運行完美
- 鉻指着臨時服務器IIS運行完美
- 瀏覽器指向本地網絡服務器都運行完美
在這一點上我有4分之3的方式工作的偉大,IE 8很爛。所以現在我改變了操作方法的結果。
回報動作方法設定的ActionResult
響應沒有指定內容的類型,AJAX調用的contentType:應用/ JSON;字符集= UTF-8
- IE 8指向登臺服務器IIS提示
- IE 9指向下載文件到臨時服務器IIS在所有
- Chrome並沒有指向臨時服務器IIS什麼事情都不做所有
- 瀏覽器指向本地網絡服務器做什麼都沒有
在這個時候,我相信有中的contentType Ajax調用本身是壞的規定。
application/json;字符集= UTF-8響應規定,對AJAX調用任何內容類型
- IE 8指向登臺服務器IIS提示
- IE 9指向下載文件到臨時服務器IIS運行完美
- 鉻指着臨時服務器IIS運行完美
- 瀏覽器指向本地網絡服務器都運行完美
我甚至增加了腳本文件JSON2.js到項目,並參考了在視圖中的腳本,這並沒有做任何事情。
這裏有一個瘋狂的部分,我有另一個控制器設置相同的方式,它構建的JSON完全一樣,唯一的區別是調用ajax的視圖略微不同地調用它。這是Ajax方法:
function setApprovalStatus(username, isApproved) {
$.ajax({
url: "/ELMAdmin/SetApprovalStatus/",
dataType: "json",
cache: false,
type: 'POST',
data: { username: username, isApproved: isApproved },
success: function (result) {
showManageUsersSuccessError(result);
}
});
}
它調用相同的子例程來渲染網格,唯一的區別是返回的數據。因此,我將所有文本從部分視圖中刪除,IE仍嘗試下載該文件。我覺得我已經嘗試了我所知道的一切,現在需要一些額外的眼睛來幫助指出我所希望的是明顯的。
你能給我們一個小的可重複的例子嗎? – asawyer
我會在tonite上做一些事情,使用相關的部分,但不知道如果我可以,因爲有太多的命中db。我一定會嘗試tho –
看看Opera Dragonfly/Firefox Firedebug/Chrome Webtools/...中的網絡選項卡,然後查看已完成的請求。這個請求是否正確? – Styxxy