當我發現vsdocs文件時,我想到了一種在我的JQuery文件中具有intellisense的方法來操縱我將發送到我的ASP.net MVC後端的對象。如何爲ASP.net MVC 4 Ajax請求生成JSon對象?
有點像MVC模型,但只是用於JQuery Ajax回調。
The folowing JQuery method is available here用於將點擊元素的html自定義標籤轉移到設計用於提供ajax回調的對象: 在顯示確認對話框時,將來自html對象的參數轉移到內存中的對象keept。
$.fn.MenuItemConfirmRemove = function (evt) {
savedEvt = evt;
RemoveLayoutMenu_JSonModel.DirectURL = $(this).attr("data-DirectURL"),
RemoveLayoutMenu_JSonModel.LayoutMenu_Translation_Id = $(this).attr("data-id"),
RemoveLayoutMenu_JSonModel.LayoutMenuTranslationLayoutMenuParent = $(this).attr("data-LayoutMenuTranslationLayoutMenuParent")
MenuItemAction = RemoveLayoutMenu_JSonModel;
$.fn.jqDialogConfirm(cap_RemoveMenuItemConfirmTitle, cap_RemoveMenuItemConfirmMessage, cap_RemoveMenuItemConfirmOkButtonTitle, cap_RemoveMenuItemConfirmCancelButtonTitle, "$.fn.MenuItemRemove");
evt.preventDefault();
}
$ .fn.MenuItemRemove是當用戶單擊確定按鈕時由通用對話框調用的方法。 The generic dialog is available here
$.fn.MenuItemRemove = function() {
$('#dialogMessage').dialog('close');
ajaxData = JSON.stringify(MenuItemAction);
$.ajax(
{
type: "POST",
url: "/DynaStructure/LayoutMenuItemRemoveItem/" + $("#hidLanguage").val(),
data: ajaxData,
contentType: "application/json",
async: false,
success: $.fn.MenuUpdated,
error: function (xhr, ajaxOptions, thrownError) {
$.fn.jqDialogAlert(DialogErrorTitle, (xhr.responseText));
}
});
}
正如你所看到的,MenuItemAction前的確認對話框被加載並打開被填滿。
MenuItemAction(全局聲明)填充有RemoveLayoutMenu_JSonModel
然後,在控制器,I必須使用C#對象,以接收此jQuery對象那樣:
/// <summary>
/// Remove a menu
/// Warning, Removing a Menu remove also its translations
/// </summary>
/// <param name="layoutMenu"></param>
/// <returns></returns>
[HttpPost, Authorize(Roles = "Admin")]
public JsonResult LayoutMenuItemRemoveItem(string language, RemoveLayoutMenu_JSonModel ajaxModel)
{
JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
string eMethod = eMethodBase + "LayoutMenuItemRemoveItem[POST]";
object eParameters = new { ajaxModel = ajaxModel };
string defaultLanguage = ConfigurationManager.AppSettings["DefaultLanguage"];
MembershipUser currentUser = Membership.GetUser();
LayoutMenu_Translation dbLayoutMenu_Translation;
using (DataRepositories _dataContext = new DataRepositories())
{
Language dbLanguage = _dataContext.Language_Rep.Query(x => x.Description == language).FirstOrDefault();
if (dbLanguage == null)
{
throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, string.Format(Resources.ErrMsg_LayoutMenuItemRemove_UnknownLanguage, language));
}
using (TransactionScope TS = new TransactionScope())
{
try
{
if (ajaxModel.LayoutMenuTranslationLayoutMenuParent == null)
{
dbLayoutMenu_Translation = _dataContext.LayoutMenu_Translation_Rep.Query(x => x.Id == ajaxModel.LayoutMenu_Translation_Id && x.LayoutMenu.FK_LayoutMenu == null).FirstOrDefault();
}
else
{
dbLayoutMenu_Translation = _dataContext.LayoutMenu_Translation_Rep.Query(x => x.Id == ajaxModel.LayoutMenu_Translation_Id && x.LayoutMenu.FK_LayoutMenu == ajaxModel.LayoutMenuTranslationLayoutMenuParent).FirstOrDefault();
}
if (dbLayoutMenu_Translation == null)
{
INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, Resources.DynaStructure_MenuAction_MenuNotFound);
iex.LogIt();
throw iex;
}
LayoutMenu dbLayoutMenu = dbLayoutMenu_Translation.LayoutMenu;
//We check if parent LayoutMenu has child
if (dbLayoutMenu_Translation.LayoutMenu.LayoutMenu1.Count > 0)
{
toReturn.Data = new { @success = false, @divToUpdate = "", @html = "", @scriptToExecute = "$.fn.jqDialogAlert('My Pretty CMS',a.message,'');", @message = Resources.DynaStructure_RemoveMenu_MenuHasChildren };
}
else
{
string[] tLanguages = ConfigurationManager.AppSettings["Languages"].Split(',');
foreach (string currentLanguage in tLanguages)
{
Language dbCurrentLanguage = _dataContext.Language_Rep.Query(x => x.Description == currentLanguage).FirstOrDefault();
if (dbCurrentLanguage == null) throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, Resources.Err_LanguageNotFound);
dbLayoutMenu_Translation = dbLayoutMenu.LayoutMenu_Translation.Where(x => x.FK_Language == dbCurrentLanguage.Id).FirstOrDefault();
if (dbLayoutMenu_Translation != null)
{
List<int> Structure_PagesIdsToKill = _dataContext.Structure_Page_Rep.Query(x => x.FK_LayoutMenu_Translation == dbLayoutMenu_Translation.Id).Select(x => x.Id).ToList();
foreach (int Structure_PagesIdToKill in Structure_PagesIdsToKill)
{
Structure_Page dbStructure_PageToKill = _dataContext.Structure_Page_Rep.GetById(Structure_PagesIdToKill);
ManagedContent toRemove = dbStructure_PageToKill.ManagedContent.SingleOrDefault();
if (toRemove != null)
{
_dataContext.ManagedContent_Rep.Remove(toRemove);
_dataContext.Save();
_dataContext.Structure_Page_Rep.Remove(dbStructure_PageToKill);
_dataContext.Save();
}
}
_dataContext.LayoutMenu_Translation_Rep.Remove(dbLayoutMenu_Translation);
_dataContext.Save();
}
}
string layoutMenuNameToRemove = dbLayoutMenu.Name;
_dataContext.LayoutMenu_Rep.Remove(dbLayoutMenu);
_dataContext.Save();
Logger.Info(enLog.Business, string.Format("User [{0}] removed layout menu [{1}]", (currentUser == null ? "#Anonymous#" : currentUser.UserName), layoutMenuNameToRemove));
}
TS.Complete();
}
catch (INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException iex)
{
TS.Dispose();
toReturn.Data = new { @success = false, @scriptToExecute = "$.fn.jqDialogAlert('My Pretty CMS',a.message,'');", @message = iex.Message };
return toReturn;
}
catch (Exception ex)
{
TS.Dispose();
INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
iex.LogIt();
toReturn.Data = new { @success = false, @scriptToExecute = "$.fn.jqDialogAlert('My Pretty CMS',a.message,'');", @message = ex.Message };
return toReturn;
}
}
string newMenuBar = RenderMenuBar(_dataContext, dbLanguage, defaultLanguage);
toReturn.Data = new { @success = true, @divToUpdate = "#divMenuBar", @html = newMenuBar, @scriptToExecute = "$.fn.DocReady();", @message = "" };
}
return toReturn;
}
The source of the ASP.net MVC 4 controller is here
我再說一遍,這個問題...
您是否嘗試生成基於C#類的JSon對象並使用它來查看當你使用它的時候你的javascript代碼中有intellisense?