如何在 ASP.NET MVC 中简化我的有状态交错模式对话框 [英] How to simplify my statefull interlaced modal dialogs in ASP.NET MVC

查看:20
本文介绍了如何在 ASP.NET MVC 中简化我的有状态交错模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以渐进增强方式在多对多模态对话框中保持状态在 ASP.NET MVC 项目中.
在我的代码中,当 javascript 被禁用时,模态对话框将导航转到另一个页面并返回,但是当启用 javascript 时,该对话框作为 jquery 模态对话框打开,就可以了.
我正在使用 这个 从点击视图中选择动作的方法.
下面的代码显示了一个主页面调用详细信息页面,有视图和控制器.只有一个主调用一个细节对话框,但我有另一个视图/控制器,其中一个主可以调用许多不同的细节对话框,有时一个对话框可以像母版页一样调用另一个嵌套的对话框.一切都必须在调用之间保持状态.

问题是它非常复杂,有很多代码来保持状态和管理对话框,我需要到处重复相同的javascript和控制器代码,我希望有一些方法可以简化它.
在视图方面需要将脚本通用以移动到单独的 .js 文件并保持查看最少的 javascript.
在控制器方面,我搜索了很多通用方法,例如过滤器或自定义绑定器,但找不到.

控制器

//##################################################################使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;使用 HYW.Models;使用 HYW.Helpers;命名空间 HYW.Controllers{公共类 TesteController :控制器{//-------私有对象 getValue(字符串键){返回会话[key];}private void setValue(string key, object value){会话[键] = 值;if (value == null) { Session.Remove(key);}}//-------[接受动词(HttpVerbs.Get)]公共 ActionResult createitem(){setValue("item", null);setValue("详细信息", null);返回视图(项目");}//-------[HttpPost][验证AntiForgeryToken][HlpFltButtonSelector(Name = "action", Argument = "saveitem")]公共 ActionResult 保存项(testePg01 模型){ModelState.Clear();返回视图(项目",模型);}//-------[HttpGet]公共操作结果 opendialog(){ModelState.Clear();testePg02 p2 = (testePg02)getValue("detail");if (p2 == null) { p2 = new testePg02();}返回视图(详细信息",p2);}//-------[HttpPost][验证AntiForgeryToken][HlpFltButtonSelector(Name = "action", Argument = "opendialog")]公共 ActionResult 打开对话框(testePg01 模型){ModelState.Clear();setValue("item", 模型);testePg02 p2 = (testePg02)getValue("detail");if (p2 == null) { p2 = new testePg02();}返回视图(详细信息",p2);}//-------[HttpPost][验证AntiForgeryToken][HlpFltButtonSelector(Name = "action", Argument = "savedialog")]公共操作结果保存对话框(testePg02 模型){ModelState.Clear();setValue("详细信息", 模型);testePg01 p1 = (testePg01)getValue("item");if (p1 == null) { p1 = new testePg01();}p1.p02 = 型号;返回视图(项目",p1);}//-------[HttpPost][验证AntiForgeryToken][HlpFltButtonSelector(Name = "action", Argument = "canceldialog")]公共 ActionResult 取消对话框(testePg02 模型){ModelState.Clear();testePg01 p1 = (testePg01)getValue("item");setValue("详细信息", null);if (p1 == null) { p1 = new testePg01();}p1.p02 = 空;返回视图(项目",p1);}//-------}}//###################################################################

查看

@model HYW.Models.testePg01@{ViewBag.Title = "ITEM";}<script type="text/javascript">//------------------------------------------------——var url_trg = '@Url.Content("~/Teste/opendialog")';var url_prl = '@Url.Content("~/Images/waitplease.gif")';//------------------------------------------------——函数 onloadpartial() {configDetailDialog(url_trg, "#tempcontent", "section[id='main']", "Detail", "#opendialog");}//------------------------------------------------——功能 configDetailDialog(trgurl, containerselector, contentselector, dlgtitle, buttonselector) {//-------$(document).ajaxError(功能(事件,jqXHR,ajaxSettings,throwedError){alert('[事件:' + 事件 + '], ' +'[jqXHR:' + jqXHR + '], ' +'[jqXHR_STATUS:' + jqXHR.status + '], ' +'[ajaxSettings:' + ajaxSettings + '], ' +'[thrownError:' + thrownError + '])');});//-------$.ajaxSetup({ cache: false });//-------$(buttonselector).click(function (event) {event.preventDefault();openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle);});//-------}//------------------------------------------------——函数 openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle) {$.ajax({类型:'获取',网址:trgurl,上下文:document.body,成功:功能(数据){var dlg = $(data).find(contentselector);$('#dlgdetail').remove();$(containerselector).append("<div id='dlgdetail'/>");$('#dlgdetail').append(dlg);$('#dlgdetail').css(边框",实体").对话({自动打开:真,模态:真,标题: dlgtitle,打开:函数(){configDetailDialog();},关闭:函数(事件,用户界面){$('#dlgdetail').remove();}}).find("form").submit(function (event) {警报('clicou' + 事件);var form = $(this);var 系 = "http://" + window.location.host + trgurl;var fdata = form.serialize() + "&action:savedialog=savedialog";$.ajax({类型:POST",网址:派系,数据:fdata,成功:功能(结果){警报(结果);}});event.preventDefault();$('#dlgdetail').dialog('close');});}});}//------------------------------------------------——<div id='tempcontent'>

<div id="formcontent">@Html.ValidationSummary(true, "Erro na pagina.")@using (Html.BeginForm()){@Html.AntiForgeryToken()<div><字段集><图例>项目</图例><div class="editor-label">@Html.LabelFor(m => m.p01campo01)

<div class="editor-field">@Html.TextBoxFor(m => m.p01campo01)@Html.ValidationMessageFor(m => m.p01campo01)

<div class="editor-label">@Html.LabelFor(m => m.p01campo02)

<div class="editor-field">@Html.TextBoxFor(m => m.p01campo02)@Html.ValidationMessageFor(m => m.p01campo02)

<p><input type="submit" style="background: #ffffff url('@Url.Content("~/Images/img01.png")')" value="opendialog" name="action:opendialog" id=打开对话框"/><input type="submit" style="background: #ffffff url('@Url.Content("~/Images/img02.png")')" value="saveitem" name="action:saveitem" id=动作:保存项目"/></p></fieldset>

}

解决方案

这里没有人真的想帮你 哈哈!
有人这里发现了一个名为 jqgrid-for-plsql 的应用程序可以帮助您提出一些想法的谷歌代码.
有人这里 讲述一些你可以发现的问题.
让 jqgrid 在视图方面为您完成所有工作,而忘记 javascript 独立性.
jqGrid 是免费的,自动创建带有分页、内联 CRUD、元数据生成模态形式 CRUD、嵌套网格、支持网格和版本中的 FK 文件的网格.
有人这里正在讲述另一个名为aspnetawesome.
有人这里在讲述这些中的验证语境.

I need keep state on many-to-many modal dialogs in a progressive enhancement way in ASP.NET MVC project.
In my code when javascript is disabled modal dialog turn in navigation to another page and return, but when javascript is enabled the dialog open as a jquery modal dialog, its OK.
Im using this method to select action from click on view.
The code below show one master page calling detail page, there is the view and the controller. There is only one master calling one detail dialog but i have another views/controllers where one master can call many different detail dialog and sometimes one dialog can behave like a master page and call another dialog nested. Everything must keep state between calls.

The problem is its very complex, there is lots of code to keep state and manage dialog, i need repeat the same javascript and controller code everywhere, i wish some way to simplify it.
In view side need turn scripts generic to move to separate .js file and keep on view a minimum of javascript.
In controller side i search a lot for some generic way to do it like a filter or custom binder but cant find.

CONTROLLER

//######################################################################  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYW.Models;
using HYW.Helpers;

namespace HYW.Controllers
{
    public class TesteController : Controller
    {
        //-------
        private object getValue(string key)
        {
            return Session[key];
        }
        private void setValue(string key, object value)
        {
            Session[key] = value;
            if (value == null) { Session.Remove(key); }
        }
        //-------
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult createitem()
        {
            setValue("item", null);
            setValue("detail", null);
            return View("item");
        }
        //-------
        [HttpPost]
        [ValidateAntiForgeryToken]
        [HlpFltButtonSelector(Name = "action", Argument = "saveitem")]
        public ActionResult saveitem(testePg01 model)
        {
            ModelState.Clear();
            return View("item", model);
        }
        //-------
        [HttpGet]
        public ActionResult opendialog()
        {
            ModelState.Clear();            
            testePg02 p2 = (testePg02)getValue("detail");
            if (p2 == null) { p2 = new testePg02(); }
            return View("detail", p2);
        }
        //-------
        [HttpPost]
        [ValidateAntiForgeryToken]
        [HlpFltButtonSelector(Name = "action", Argument = "opendialog")]
        public ActionResult opendialog(testePg01 model)
        {
            ModelState.Clear();
            setValue("item", model);
            testePg02 p2 = (testePg02)getValue("detail");
            if (p2 == null) { p2 = new testePg02(); }            
            return View("detail", p2);
        }
        //-------
        [HttpPost]
        [ValidateAntiForgeryToken]
        [HlpFltButtonSelector(Name = "action", Argument = "savedialog")]
        public ActionResult savedialog(testePg02 model)
        {
            ModelState.Clear();
            setValue("detail", model);
            testePg01 p1 = (testePg01)getValue("item");            
            if (p1 == null) { p1 = new testePg01(); }
            p1.p02 = model;            
            return View("item", p1);
        }
        //-------
        [HttpPost]
        [ValidateAntiForgeryToken]
        [HlpFltButtonSelector(Name = "action", Argument = "canceldialog")]
        public ActionResult canceldialog(testePg02 model)
        {
            ModelState.Clear();
            testePg01 p1 = (testePg01)getValue("item");
            setValue("detail", null);
            if (p1 == null) { p1 = new testePg01(); }
            p1.p02 = null;
            return View("item", p1);
        }
        //-------
    }
}
//######################################################################  

VIEW

@model HYW.Models.testePg01
@{
    ViewBag.Title = "ITEM";
}
<script type="text/javascript">
    //-------------------------------------------------
    var url_trg = '@Url.Content("~/Teste/opendialog")';
    var url_prl = '@Url.Content("~/Images/waitplease.gif")';
    //-------------------------------------------------
    function onloadpartial() {
        configDetailDialog(url_trg, "#tempcontent", "section[id='main']", "Detail", "#opendialog");
    }
    //-------------------------------------------------
    function configDetailDialog(trgurl, containerselector, contentselector, dlgtitle, buttonselector) {
        //-------
        $(document).ajaxError(
            function (event, jqXHR, ajaxSettings, thrownError) {
                alert('[event:' + event + '], ' +
                        '[jqXHR:' + jqXHR + '], ' +
                        '[jqXHR_STATUS:' + jqXHR.status + '], ' + 
                        '[ajaxSettings:' + ajaxSettings + '], ' +
                        '[thrownError:' + thrownError + '])');
            });
        //-------
        $.ajaxSetup({ cache: false });
        //-------
        $(buttonselector).click(function (event) {
            event.preventDefault();
            openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle);
        });
        //-------
    }
    //-------------------------------------------------
    function openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle) {
        $.ajax({
            type: 'GET',
            url: trgurl,
            context: document.body,
            success: function (data) {
                var dlg = $(data).find(contentselector);
                $('#dlgdetail').remove();
                $(containerselector).append("<div id='dlgdetail'/>");
                $('#dlgdetail').append(dlg);
                $('#dlgdetail')
                    .css("border", "solid")
                    .dialog({
                        autoOpen: true,
                        modal: true,
                        title: dlgtitle,
                        open: function () {
                            configDetailDialog();
                        },
                        close: function (event, ui) {
                            $('#dlgdetail').remove();
                        }
                    })
                    .find("form").submit(function (event) {
                        alert('clicou ' + event);
                        var form = $(this);
                        var faction = "http://" + window.location.host + trgurl;
                        var fdata = form.serialize() + "&action:savedialog=savedialog";
                        $.ajax({                            
                            type: "POST",
                            url: faction,
                            data: fdata,
                            success: function (result) {
                                alert(result);
                            }
                        });
                        event.preventDefault();
                        $('#dlgdetail').dialog('close');
                    });
            }
        });
    }
    //-------------------------------------------------
</script>
<div id='tempcontent'>
</div>
<div id="formcontent">
    @Html.ValidationSummary(true, "Erro na pagina.")
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div>
            <fieldset>
                <legend>Item</legend>
                <div class="editor-label">
                    @Html.LabelFor(m => m.p01campo01)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.p01campo01)
                    @Html.ValidationMessageFor(m => m.p01campo01)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(m => m.p01campo02)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.p01campo02)
                    @Html.ValidationMessageFor(m => m.p01campo02)
                </div>
                <p>
                    <input type="submit" style="background: #ffffff url('@Url.Content("~/Images/img01.png")')" value="opendialog" name="action:opendialog" id="opendialog" />
                    <input type="submit" style="background: #ffffff url('@Url.Content("~/Images/img02.png")')" value="saveitem" name="action:saveitem" id="action:saveitem" />
                </p>
            </fieldset>
        </div>
    }
</div>

解决方案

nobody really want to help you here lol !
Someone here found a application called jqgrid-for-plsql on google code that can help you with some ideas.
Someone here telling about some problems you can found with it.
Leave jqgrid do all job for you on view side and forget about javascript independence.
jqGrid is free and automatically create the grid with pagging, inline CRUD, metadata generated modal form CRUD, nested grids, support for FK fileds in grid and edition.
Someone here is telling about another library called aspnetawesome.
Someone here is telling about validation in these context.

这篇关于如何在 ASP.NET MVC 中简化我的有状态交错模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆