1

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>
Community
  • 1
  • 1
newway
  • 647
  • 1
  • 13
  • 21
  • Use knockout, there is how to : http://stackoverflow.com/questions/7436160/mvc-knockout-js-inside-jquery-dialog –  Apr 02 '12 at 14:42
  • if using knockout see that: http://stackoverflow.com/questions/5741658/knockout-mvc-3-validation/6559734#6559734 –  Apr 03 '12 at 18:28
  • There is some samples there: http://stackoverflow.com/questions/11534788/mvc-c-sharp-modal-popup http://stackoverflow.com/questions/3711576/add-a-search-box-to-a-master-page –  Sep 10 '12 at 18:48
  • Im the owner of question and with the answer deletions did by the bot i losted a lot of userfull information to me. I comment about it on meta on this page: http://meta.stackexchange.com/questions/7505/please-sort-deleted-answers-to-the-bottom#comment420670_7505 – newway Sep 12 '12 at 14:46
  • There is it too: http://jsfiddle.net/rniemeyer/MJQ6g/ –  Apr 02 '12 at 16:01

1 Answers1

2

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.

Community
  • 1
  • 1
  • Hi thanks, i was losting hope someone see it and wrote something. Now i forgot javascript independence and im writing all by hand. – newway Feb 03 '12 at 23:11