Html.EditorFor, Model Property vs RouteData Value

Filed Under (Misc) by Emad Alashi on 13-05-2012

Tagged Under : , , ,

The ASP.NET MVC team made our lives easier when they created the Html editor extension method Html.EditoFor(); you just pass the model property and it creates the right editor, filling it with the property’s value…but not always!

Let’s consider that we have the conventional route definition:

routes.MapRoute(

                "Default", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

            );

and this simple action method:

public ActionResult MyAction(int? id)

{

    var result = new Person() { Id = 3, Name = "My Name" };

 

    return View(result);

}

and this form view:

@using (Html.BeginForm())

{

    @Html.EditorFor( m => m.Id )

    <br />

    @Html.EditorFor( m => m.Name )

    <br />

    <input type="submit" value="Submit" />

}

If you have noticed, in the action method “MyAction” I didn’t make use of the passed “id” parameter; I know this is a rare case, but maybe for a rare business scenario you want to return a very similar model rather than the requested one. In our case we will return the model with the id = 3 no matter what, just for the sake of the argument.

Now if you call for the request “http://localhost:2085/MyController/MyAction” everything works as expected, and you will have the following view result:

CropperCapture[5] 

But if  you call for the request “http://localhost:2085/MyController/MyAction/5”, the result will be:

CropperCapture[6]

As you have noticed, the Html.EditorFor ignored the value of the property “id” in the model, and instead used the RouteData value of the key “id” passed through the URL.

Most likely this is by design; I can’t think of how a bug in the code could produce this. But according to how I see things, this can be really confusing; I should be expecting the value of the property I passed instead of the routedata value.

What do YOU think?

ثلاثاء عمان التقني – الذكرى الثانية

Filed Under (software management) by Emad Alashi on 05-05-2012

Tagged Under : , , ,

548151_10150784486075210_599395209_11585084_1539081719_nثلاثاء عمان التقني لمن لا يعرفه هو لقاء تقني يعقد كل أول يوم ثلاثاء من كل شهر، يتناول مواضيع مختلفة في كل مرة تحوم كلها حول التقنية و استخداماتها، يتم فيه استضافة متخصصين في موضوع تقني معين يطرحون آراءهم في أهم المستجدات في الموضوع المختار أمام جمهور كبير من المتحمسين. يقوده طليعة من الشباب الفطن الجاد.

و صادف الثلاثاء الماضي الذكرى الثانية لثلاثاء عمان التقني، الذي أقاموا فيه معرضا للشركات الناشئة، و الذي كنت من المحظوظين بزيارته.
بصراحة لم أكن أتوقع ما رأيت؛ أكشاك متراصة لشركات ناشئة ذات حماس منقطع النظير! و لم يكن الحماس فقط ما ميزها، بل المستوى العالي من الاحترافية في منتجاتهم و خدماتهم –لأغلبها- و وضوح الرؤية، و بعد النظر، و إدراك السوق، و استيعاب فكرة التميز عن الأخرين في خصائص الخدمات، و قدرتهم على عرضها بشكل غير تقليدي و بجاذبية ملفتة للنظر.بالإضافة لبعض الأفكار الإلكتورنية الملموسة مثل “مجس الغاز” (تعريبي الخاص) و التحكم بالآلة من خلال Kinect.
فاجأني كل هذا و شعرت بفخر و أمل كبيرين.

كنت أمر على الكشك و أستعرض خدمة الشركة الناشئة و ما تقدم، و من ثم أبادر بأسئلة أتحدى فيها وجودهم مثل : “كيف تتميزون عن غيركم و هناك من يعرض خدماتكم نفسها؟ ماذا لو كنت زبونا عندكم و حصل معي كذا، كيف تستجيبون لمثل هذا الإشكال؟ كيف أضمن جودة خدماتكم؟ ما هي خطوتكم التالية و ما هي رؤيتكم؟” و في المعظم كانت أجوبة متينة بالنسبة لشركة ناشئة، تظهر مدى تمسكهم و جديتهم بالاستمرار و النجاح.

و لكن لا بد أيضا من سرد بعض الملاحظات و النصائح، و ها هي قائمتي:

  1. كان هناك أكشاك فارغة بأسماء شركات ناشئة لم تستغل فرصتها و لم تحضر المعرض، أضاعوا فرصة كبيرة لتسويق خدماتهم و شركاتهم، و أضاعوا الفرصة على غيرهم، و يشكك هذا بقدرتهم أصلا على إنتاج خدمات يستطيع الزبائن الثقة بها و الاعتماد عليها
  2. إذا كنت صاحب كشك في معرض، لا تنتظر أن يتقدم أحدهم فيسأل، بادر أنت باستضافته، أظهر له الحماسة و الاهتمام به؛ إذا لم تكن مهتما و أنت صاحب الفكرة، فهل سيهتم عابر سبيل؟ رأيت أثر هذا علي مباشرة و أنا أمشي بين الأكشاك
  3. إذا كنت صاحب كشك في معرض، لا تجعل بينك و بين الزوار حاجزا كطاولة مثلا، فأنت “مع” الزائر “أمام” المنتج، أنت في صفه و حليفه، و لست في الطرف الآخر، يبدو أمرا سخيفا لكن في الواقع له أهمية نفسية كبيرة
  4. الألوان و التصاميم الجذابة تزداد أهمية يوما بعد يوم، و أثرها جد واضح في تكاثر الناس حولها. ابتعد عن التصاميم “الصلبة” التقليدية للشركات الكبرى، الآن عصر “الكاجوال” نوعا ما، لكن باتزان Smile
  5. من الأفضل أن تستثمر قليلا بالأوراق الدعائية، طباعة ذات جودة جيدة مع ألوان أفضل بكثير من A4 أبيض و أسود
  6. استخدام الأفكار الجديدة في جلب الانتباه أمر جميل، لكن احذر من أن تكون مخترفة للخصوصية، أو مبهمة بحيت يتردد الزائر من الاشتراك فيها، فلن أقف لأتصور و أنا أرسم رسمة لا أفهم المغزى من رسمي لها دون أن تشرح لي (اسحب ورقة وارسمها) لماذا؟ كيف سيفيد هذا في دعايتكم و تعريفنا بكم و بخدمتكم؟
  7. أنت لست بائعا متجولا، لا تختر طريقة عرض رتيبة و بكلمات مصطنعة مرتبة، كن تلقائيا و لتكن كلماتك من القلب، بعضهم نجح في ذلك حتى أنني وجدت أنه من اللائق أن أغادر كشكهم لأني أخذت من وقتهم و من وقت المهتمين الأخرين، لم أرد أترك المكان بسهولة
  8. و أخيرا إذا كنت زائرا لمعرض، فاعلم أن وقتك ثمين، وستتعب من المشي، و حضر نفسك لحمل الكثير من الأوراق الداعئية التي ستثقل كاهلك

شكرا للقائمين على ثلاثاء عمان التقني، أبدعتم كالعادة و أكدتم أن الشباب العربي لديه القدرة، و الحماسة، و التصميم على إنتاج خدمات و شركات ذات جودة و رؤية ناضجة. شكرا شكرا.

ملاحظة 1: في اللحظة التي سأحصل فيها على قائمة المشاركين سأجدد هذه التدوينة و أدرجها فيها، على كل حال الصورة في الأسفل تظهر بعضها

ملاحظة 2: للأسف لم يتسن لي التقاط صور للمعرض، فسأكتفي بهذه الصورة التي تعرض بعض الأوراق الدعائية منه.

fliers

How Html.Action() Work

Filed Under (Development) by Emad Alashi on 23-03-2012

Tagged Under : , , , ,

Let’s take the example:

@Html.Action("Latest", "Episode")

What this will do is to invoke the “Latest” action method in the “Episode” controller. But what really happens behind the scenes is NOT a direct invoke; it will actually start from the beginning of the ASP.NET MVC execution pipeline using “Latest” and “Episode” as Route values for the keys “action” and “controller” respectively.

This means that you should pay very good attention to your Routes definition in the Application_Start() in Global.asax; Html.Action() will try to match the best route in your defined routes according to the RouteValueDictionary created above (action and controller) along with any additional route values provided in the overload.

So bottom line don’t assume that Html.Action will invoke action directly, and make sure that your Route unit tests always cover your back when you need to change your Route definitions, or your risk your Html.Action() methods to be ruined.

The Magical Effect of Simple Encouraging Words

Filed Under (Development, Misc, Personal, software management) by Emad Alashi on 21-03-2012

Tagged Under : , , ,

image credit goes for www.discoveryeducation.com/Recently I had to go through some health checkup that included tests for my eye, one of these tests required from me to keep my eye open for a long period of time concentrating continuously into an extremely strong and annoying light.
During the test I kept receiving encouraging words from the examining doctor every couple of seconds: “Bravo Emad! very good concentration, keep it up, that’s it”.
Even though it was a simple task and me being in my 30’s!… I still had a strong feeling that I was doing something right! and that I should keep doing what I was doing, to give more effort no matter how annoying and hurting this intense light was; and all of this was due to his words! the more he encouraged the wider I opened my eye and concentrated.

The moral of the story is that if you are managing people never ever under estimate the simple words of encouragement, or discouragement!

Criticizing Google Reader’s UI

Filed Under (Misc) by Emad Alashi on 13-03-2012

Tagged Under : , , ,

Since Google changes the look of its products very often, most likely this post will be out of date soon, nonetheless I believe it’d be a good to share my thoughts about Google Reader’s current UI… with a little rant.

  1. Lots of real-state waste:
    All the areas surrounded with yellow borders are a wasted, especially that I have no control over hiding or showing them (click on image to see it real size):

    overallRealState2-edited

  2. Actions RARELY used are ALWAYS available, and taking too much space: ActionsRarelyUsed-edited
  3. Actions most used are small, at the bottom of the post, and not always in view:NeededActionsNotAvailable
  4. The highlight of the selected blog isn’t clear enough:

    HighlightingSelectedBlogIsWeak
  5. Title doesn’t stay in view while scrolling (negotiable): titleMovesWithScrolling-edited

Ok Google you did a great job introducing GMail and Reader with all this labeling and cool stuff, how about little bit attention to UI?

QueryString in Routing and Model Binding in ASP.NET MVC

Filed Under (Development) by Emad Alashi on 13-01-2012

Tagged Under : , , , , ,

In a nutshell, a query string will not be part of the RouteData dictionary in the routing process, yet it will affect the route matching. On the other hand when Model Binding takes place, query string will be used to bind to the parameters of the action method, unless there is an item in route data with the same key.
Now the details, let’s consider this ASP.NET MVC application, we have an action method:
public ViewResult Archive(DateTime? dateFrom, int? page){...
And we have defined the following routes:
routes.MapRoute(

          "Archive",

          "Archive/{dateFrom}",

          new { controller = "Episode", action = "Archive", dateFrom = UrlParameter.Optional}

          );

Let’s check the URL “~/Archive/1-2-2012?page=2”:
The RouteData dictionary will be as the following:
Key Value
controller Episode
action Archive
dateFrom 1-2-2012?page=2

Yet the action method above will work, and be invoked with the parameters:

Parameter Value
dateFrom 1-2-2012
page 2

So let’s notice the following:

  1. The query string “page” key is NOT part of the RouteData dictionary
  2. In the routing process “?page=2” is considered to be part of the value of the “dateFrom” item in the RouteData dictionary (watch out your route unit tests!)
  3. Even though “dateFrom” route item has the value “1-2-2012?page=2”, Model Binding figures that it can ignore the “?page=2” part because it’s a query string, and the dateFrom parameter is bound correctly to the “1-2-2012”
  4. Even though there is NO “page” item in the RouteData dictionary, the query string it is still used in the Model Binding to bind to the “page” parameter

Having this explained and the above notes taken in mind, what if we have defined an item “page” in the route itself at the end like the following:

routes.MapRoute(

    "Archive",

    "Archive/{dateFrom}",

     new { controller = "Episode", action = "Archive", dateFrom = UrlParameter.Optional, page = 1}

     );

Then this item “page” will show in the RouteData but it should NOT be mistaken by the query string “page”, the Model Binding will always ignore the query string and use the route data item to bind to the action parameter “page”.

And this is why I am writing this post:

  1. I have mistaken the route item “page” with the query string “page” and this confused me a lot
  2. My routing unit tests failed because the query string was still concatenated and present in the value of the route data item, I should have ignored them

I hope this helps you better understand how query strings affects Routing and Model Binding, and helps you avoid what I have went through. Watch out for routing unit tests, and good luck.

Interviews with Jordanian Microsoft Guests

Filed Under (Development, software management) by Emad Alashi on 31-12-2011

Tagged Under : , , , , ,

During the last quarter of 2011 we have hosted two amazing Jordanian Microsoft guests, Yousef Al Khalidi (Distinguished Engineer), and Ayman Dahleh (Development Manager for the Global Experience Platform group), who happened to be having their vacation here, they generously accepted our invitation to deliver sessions here in Jordan despite their busy times.

The community recorded these two inspiring interviews with our guests through which I can see how close achievements can be.

A big thank-you to Mr. Yousef and Mr. Ayman for their availability, and thanks to our DPE at the time Mohammad Arrabi for giving us this opportunity to meet such great people, and thanks to our community member Mosab for making these videos.

Hide Subfolder in Multiple Domains Under One Site

Filed Under (Development, DotNetArabi) by Emad Alashi on 26-12-2011

Tagged Under : , ,

After I have moved to WinHost, I was surprised they don’t support multiple domains under one site out of the box; you have to rely on IIS URL Rewrite magic to achieve that.

So as expected, I looked for a solution in their KB and the internet (you don’t reinvent the wheel, remember?) and found couple of posts here, here, here, and here that helped a lot in achieving this.
So I managed to have www.emadashi.com and www.dotnetarabi.com working fine together residing in the folders “emadashi-blog” and “dotnetarabi-root” respectively under the root; they both have same IP address, but upon request a redirection is made to the proper application folder.

Until one day my very good friend Omar Qadan generously shared a link to an episode he found interesting; and the link was as the following:

“www.dotnetarabi.com/dotnetarabi-root/episode.aspx?..etc”

Ops! that’s not right isn’t it! The URL should not include the folder name “dotnetarabi-root”! So I read more about Regular Expressions, read the above mentioned article more thoroughly, and came to the conclusion that for each domain we have we are going to write two rules:

1) Correct and Redirect undesired URL’s:
If IIS receives a request that contains the folder name (e.g “www.dotnetarabi.com/dotnetarabi-root/episode.aspx”), then:

  1. Omit the folder name from the URL, in our case the URL becomes “www.dotnetarabi.com/episode.aspx” 
  2. Redirect the request again to IIS using the new URL, by sending 301 status prompting the browser to initiate a new request with the new URL

The step above can be achieved by the following:

Figure 1:

    1. <rule name="UnWantedDirectAccessToSubFolder-DotNetArabi-root" patternSyntax="ECMAScript" stopProcessing="true">
    2. <match url=".*" />
    3. <action type="Redirect" url="{C:1}" appendQueryString="true" logRewrittenUrl="false" />
    4. <conditions>
    5. <add input="{HTTP_HOST}" pattern="^(www.)?dotnetarabi.com" />
    6. <add input="{PATH_INFO}" pattern="^[/\\]dotnetarabi_root[/\\](.*)" />
    7. </conditions>
    8. </rule>

Then we do the second rule:

2) Guide and Redirect desired URL’s to the right folder:
If IIS receives a request by the URL format we desire, which doesn’t contain the folder name (e.g. “www.dotnetarabi.com/episode.aspx”) then:

  1. Insert the folder name to the URL to be come “www.dotnetarabi.com/dotnetarabi-root/episode.aspx” in our case
  2. Rewrite the URL again within IIS using the new URL

Which can be achieved by the following:

Figure 2:

    1. <rule name="DirectToDotNetArabiRoot" patternSyntax="ECMAScript" stopProcessing="true">
    2. <match url=".*" />
    3. <action type="Rewrite" url="dotnetarabi_root/{R:0}" appendQueryString="true" logRewrittenUrl="false" />
    4. <conditions>
    5. <add input="{HTTP_HOST}" pattern="^(www.)?dotnetarabi.com" />
    6. <add input="{PATH_INFO}" pattern="^[/\\]emadalashi_blog[/\\]" negate="true" />
    7. <add input="{PATH_INFO}" pattern="^[/\\]dotnetarabi_root[/\\]" negate="true" />
    8. </conditions>
    9. </rule>

The best way to describe what happens is through this diagram I put together (the start block is the “Browser”):

clip_image001

And that was it, now notice the following:

  1. The rules should maintain the mentioned order
  2. Rule 1 uses action type “Redirect”, and Rule 2 uses action type “Rewrite
  3. We configure the rules to stop processing any rule after it is executed
  4. Your key to successful and easy manipulation of the URL is to understand the Back-references in the URL Rewriting, notice the {C:1} and {R:0} usage above, notice as well the brackets () in the URL’s; they are used to capture the back-references, check the “Using back-references in rewrite rules” in the above mentioned article
  5. To test your rules use Fiddler, IE Developers Tools (Network tab), or Firebug (Net tab), and the great tool IIS provides to test rules which can be accessed by: open IIS –> select site-> URL Rewrite –> Double click rule –> double click condition –> Test pattern:
    clip_image002

I hope you found this useful.

Let me do thanks here to  www.gliffy.com as well for providing such great service for drawing diagrams online

Microsoft’s Support

Filed Under (software management) by Emad Alashi on 16-12-2011

Tagged Under : ,

microsoft-logoCouple of days ago I tried a key I acquired from Microsoft MSDN subscription to activate an Office installation, the activation didn’t work for some reason, so I filed a ticket, and I thought to myself “Oh great! now I will have to wait for ages until such large corporate like Microsoft would answer my ticket!”

And less than 48 hours, I don’t receive an email, but my phone rings! The guy on the phone answers my inquiry and makes sure I am answered.
Considering how large this corporate is and the number of its clients, this is really amazing!

So if you are a startup, or a growing business, please keep this in mind; don’t give excuses to yourself for a low quality of service due to your growing business.

Good job on this Microsoft.

“AmmanTT: Developer Edition” presentation and slides

Filed Under (Development, Misc, software management) by Emad Alashi on 09-12-2011

Tagged Under : ,

It was a very interesting event “AmmanTT: Developer Edition” which took place last Tuesday; the team did a very good job putting it together, it is really great seeing this great effort put into such fruitful events; moving the community steps forward, so thank you AmmanTT team, you rock.

Hereby I post the slides I presented in my talk: