Wednesday, September 26, 2012

reminder on validations in ajax mvc razor

 Order IS important and can stop the client side vlidation working if not in this order.
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


        [Required]
        [StringLength(20, MinimumLength = 4)]
        [Display(Name = "User Name")]
        [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
        [ScaffoldColumn(false)]
        public string UserName { get; set; }

        [Required]
        [StringLength(18, MinimumLength = 0)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required]
        [StringLength(18, MinimumLength = 0)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

            <div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
           
            <div class="editor-label">
                @Html.LabelFor(model => model.LastName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>


All Active Directory Users 1000 user limit

Only 1000 of my AD entries were being returned by dsUsers,FindAll(), quick search after spending what seemed hours banging my head trying to debug where I could only access a small development DC with < 100 users...So deploy to prod server and output file appends with info, yeah the long way.
It was also only returning a portion of the properties within that 1000 too, 381 entries it told me didn't have an ipPhone field set, whereas Domain Users server tools told me that 99% were set...
Anyway, found that someone already had figured out to frig the Pagesize parameter, which for some reason got past the 1000 restriction.

                    // declare directory searcher
                    DirectorySearcher dsUsers = new DirectorySearcher(deRoot);

                    dsUsers.SearchScope = SearchScope.Subtree;
                    //OR the filter
                    dsUsers.Filter = "(|(objectCategory=person)(objectClass=user))";
                    dsUsers.PageSize = 1001;// ms limitation with shitty workaround

                    // define what properties you want to have loaded into your search results
                    dsUsers.PropertiesToLoad.Add("givenName");
                    dsUsers.PropertiesToLoad.Add("sn");
                    dsUsers.PropertiesToLoad.Add("samAccountName");
                    dsUsers.PropertiesToLoad.Add("ipPhone");
                    dsUsers.PropertiesToLoad.Add("telephoneNumber");
                    dsUsers.PropertiesToLoad.Add("mobile");
                    dsUsers.PropertiesToLoad.Add("department");
                    dsUsers.PropertiesToLoad.Add("l");

Tuesday, September 11, 2012

handy dynamic JQuery event handler


in View :-
grid2.Column(format: @<input type="button" onclick="mysub='@item.UserName'" value="Setup" name="Setup" id="Setup" />),

var mysub = "";

            $(function () {
                $("#Setup").live('click', function () {
                    alert(mysub);
                });
            });

Thursday, August 30, 2012

MVC razor pain with dropdowns

Took a little head banging against the wall with this one, probably my lack of exposure to the MVC @razor model.
                <div>
                Region  @Html.DropDownListFor(model => model.Region, Model.Regions)
                </div>
Simple enough...but for one reason and another I needed to change the model, and all of a sudden the currently selected 'Region' ws no longer being default selected on a repost.
                <div>
                Region  @Html.DropDownListFor(model => model.subclass1.Region, Model.Regions)
                </div>
at first it was the incorrect property declaration :-
public string Region =""; instead of public string Region { get; set; }
still not fixed...
moved a copy of Region from model.subclass1.Region back up a level to model.Region.

Working again....so I can only surmise that the @razor model isn't too keen on properties being on sublevels when used for the selected item in a dropdown list.




Friday, August 24, 2012

Cisco AXL stuff in C# snip 2

Adding a user group to a user sounded fairly easy...took a bit of digging in the ambiguous Item..fields.You need to add user or get use before adding usergroup, not possible in same operation as add user.

                    UpdateUserGroupReq _uug_req = new UpdateUserGroupReq();
                    _uug_req.Item = "Standard CCM End Users";
                    _uug_req.ItemElementName = ItemChoiceType63.name;
                    XUserGroupMember _xugm = new XUserGroupMember();
                    UpdateUserGroupReqAddMembers _xugam = new UpdateUserGroupReqAddMembers();
                    _xugm.uuid = _sres.@return;//This is the return from addUser
                    _xugm.Item = _xuser.userid;
                    _xugam.member = new XUserGroupMember[] { _xugm };
                    _uug_req.Items = new UpdateUserGroupReqAddMembers[] { _xugam };
                    _uug_req.ItemsElementName = new ItemsChoiceType6[] { ItemsChoiceType6.addMembers };
                    _sres = _proxy.updateUserGroup(_uug_req);

Some MVC @Razor Ajax snippets

Loads of stuff around, mostly on stackoverflow.com when searching for help. But sifting through all those variations of other peoples problems that are only 'similar' to yours...

Instead of using target I wanted to return multiple partial views, dependant upon success or fail of the call (submit), I also wanted different actions with the same form data. The simplified version of the mechanism is below, and yes there are probably  a multitude of different ways to accomplish the same, but this works for me and my way of thinking.

@using (Ajax.BeginForm("CreateNew", "Home", new AjaxOptions { OnComplete = "CreateComplete", OnBegin = "CreateBegin" }))

         <input type="submit" onclick="mysub='New'" value="CreateNew" name="submitButton" id="createnew" style="visibility:hidden"/><span id="createwait" style="visibility:hidden"> Please wait..</span>
         <input type="submit" onclick="mysub='Find'" value="CreateFind" name="submitButton" id="createfind" />

        <script language="javascript" type="text/javascript">

            function CreateComplete(result) {
                if (mysub == 'New') {
                     $("#Audit").html(result.responseText);
                 }
                 if (mysub == 'Find') {
                     var _ret = result.responseText;
                     var _reta = _ret.split("<br />");
                     $("#Regions").val($.trim(_reta[0]));
                      }

            function CreateBegin() {
                $("createnew").attr("disabled", "disabled");
                $("#createnew").css({ "visibility": "hidden" });
                $("#createwait").css({ "visibility": "visible" });
            }
</script>
In my Controller action :
         [HttpPost]
        public ActionResult CreateNew(string submitButton, Index ix)
        {
            if (submitButton == "CreateNew")
            {
.....
                return PartialView("Audit", (ix.audit));
            }
            else
            {
......
                 return PartialView("Audit", (ix.region));   
            }


Just while i am here, a strange JQuery problem to pass parameters to event functions that I want to manually call...for example, if i send and Ajax request off, get some data back, and based on the data retrieved i want to update a dropdown list selected item, then populate another dropdown list and execute that changed event handler.
So from my above javascript function CreateComplete I change the selected item on the #Regions dropdown list, then I want to populate the Office dropdown by using the change event on #Regions AND use the same response as I already have to set the selected Office:-
                $("#Regions").trigger("change", $.trim(_ret));
 $("#Regions").change(function (event, _office) {
 $.getJSON("./Home/GetOfficeList", { CountryId: $("#Regions").val() }, function (data) {
               data = $.map(data, function (item, a) {
                return "<option value=" + item.Value + ">" + item.Text + "</option>";
                  });}
                var _reta = _office.split("<br />");
                $("#Offices").val(($.trim(_reta[1])).replace(' ', '_'));
                $("#Depts").val(($.trim(_reta[2])).replace(' ', '_'));

So notice how i pass the whole response to the trigger call? at first i passed the parameters i needed (Office and Dept) seperately like this :-
                $("#Regions").trigger("change", $.trim({_ret[1],_ret[2]}));
and the regions function changed to :-
                $("#Regions").change(function (event, _office,_dept)
But _dept parameter never made it accross, no matter how i packaged the calling parameters.
according to the JQuery doumentation :-
The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger() call, these parameters will be passed along to the handler as well. To pass more than one parameter, use an array as shown here. As of jQuery 1.6.2, a single parameter can be passed without using an array.







Some Cisco AXL stuff in C# Snip 1.

Referring to Cisco Unified Configuration Manager.
First off, this stuff is damned hard to find out any documentation for the API's, very few examples around, developer forums pretty bare of any answers to a multitude of questions.

This was about the most helpful page that I found on the internet to start me off on the right footing.
http://ciscoaxlguy.wordpress.com/
Problem was for me, my client was on V7.1, so a few things were not working and needed investigation. So all of my snippets will be related to V7.1, Cisco seems to break a lot between releases.

To get Extension mobility objects in to the device profile I needed to query the database directly.

                     ExecuteSQLQueryReq _eSql_req = new ExecuteSQLQueryReq();
                     ExecuteSQLQueryRes _eSql_res;
                    _eSql_req.sql = "select name,pkid from telecasterservice";
                    _eSql_res = _proxy.executeSQLQuery(_eSql_req);

Don't believe the documentation that tells you that the underlying indicator to find an Unnassigned DN from the route plan report is the 'isCallable' flag. This flag relates to the UI interface 'Active' checkbox on the line details. Unfortunately this flag does not get updated if the associated device  is deleted and the line not, which is quite often the case.
The report must actually do the following sql query:-
select dnorpattern from numplan n inner join typepatternusage as tpu on n.tkpatternusage=tpu.enum left outer join devicenumplanmap as dmap on n.pkid = dmap.fknumplan where tpu.name='Device' and dmap.fknumplan is null and dnorpattern like...
So basically trying to find an unassociated device.