MVC4 Conditional attribute(s)

MVC4 beta has conditional attributes built in...

 

<div @{if(myClass !=null){<text>class="@myClass"</text>}}>Content</div>

<div class="@myClass">Content</div>

 

If @myClass is null, it just won't use the attribute at all...

Descriptor Pattern Part II

I described the descriptor patteren earlier, this time I want to elaborate a bit showing the advantage of extending it and using it in combination with an MVC application. I created two classes ApplicationAction and Account Action where AccountAction extends ApplicationAction.

Here is the implementation of the ApplicationAction

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class ApplicationAction
{
private string _description = String.Empty;

private static readonly IDictionary dict = new Dictionary<string, ApplicationAction>();

protected ApplicationAction(string description)
{
_description = description;
dict.Add(description, this);
}

public override string ToString()
{
return _description;
}

public static ApplicationAction Parse(string description)
{
if (dict.Contains(description))
{
return dict[description] as ApplicationAction;
}
else
{
throw new NotImplementedException("This description is currently not supported.");
}
}

public static bool TryParse(string description, out ApplicationAction gender)
{
try
{
gender = Parse(description);
return true;
}
catch (NotImplementedException)
{
gender = null;
return false;
}
}

public static IEnumerable GetMembers()
{
return dict.Values.Cast().ToList();
}
}
}

Here is the AccountAction


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class AccountAction : ApplicationAction
{
private AccountAction(string description)
: base(description)
{

}

// Account
public static readonly AccountAction Login = new AccountAction("Login");
public static readonly AccountAction LogOff = new AccountAction("LogOff");
public static readonly AccountAction Register = new AccountAction("Register");
public static readonly AccountAction Disassociate = new AccountAction("Disassociate");
public static readonly AccountAction Manage = new AccountAction("Manage");
public static readonly AccountAction ExternalLogin = new AccountAction("ExternalLogin");
public static readonly AccountAction ExternalLoginCallback = new AccountAction("ExternalLoginCallback");
public static readonly AccountAction ExternalLoginConfirmation = new AccountAction("ExternalLoginConfirmation");
public static readonly AccountAction ExternalLoginFailure = new AccountAction("ExternalLoginFailure");
public static readonly AccountAction ExternalLoginsList = new AccountAction("ExternalLoginsList");
public static readonly AccountAction RemoveExternalLogins = new AccountAction("RemoveExternalLogins");
}
}

 

And when you want to use it. 

<a href="@Url.Action(@AccountAction.Login.ToString(), "Account", new { Area="" })">Login</a>

The Descriptor Pattern

 This pattern describes the descriptor pattern, this is a usefull pattern to use in MVC development. You could use it to store your actions and controllers in. In the event that the action or controller needs to be changed you can simply change is in the descriptor rather than changing it at all locations in your code.

    public class Genders
    {
        private readonly string _description = String.Empty;
        private static readonly IDictionary dict = new Dictionary<string, Genders>();
        public static readonly Genders Indifferent = new Genders("Indifferent");
        public static readonly Genders Male = new Genders("Male");
        public static readonly Genders Female = new Genders("Female");
        public static readonly Genders Unknown = new Genders("Unknown");

        private Genders(string description)
        {
            _description = description;
            dict.Add(description, this);
        }

        public override string ToString()
        {
            return _description;
        }

        public static Genders Parse(string description)
        {
            if (dict.Contains(description))
            {
                return dict[description] as Genders;
            }
            else
            {
                throw new NotImplementedException("This description is currently not supported.");
            }
        }

        public static bool TryParse(string description, out Genders gender)
        {
            try
            {
                gender = Parse(description);
                return true;
            }
            catch (NotImplementedException)
            {
                gender = null;
                return false;
            }
        }
        
        public static IEnumerable GetMembers()
        {
            return dict.Values.Cast().ToList();
        }
    }

To use it simply use.

Genders.GetMembers

or any other feature you wish to use.

Adding ViewBag value to @Html.Hidden

If you want to assing a viewbag property to an Html Extension try the following.

 

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))

 

Intellisense for CSS in VS2013 MVC

A very nice feature which is new in Visual Studio 2013 is the code completion / intellisense for MVC pages. It's a bit faded in the example, but you can see the bootstrap pull-left and pull-right.

 

 

Creating an MVC 5 Controller - Empty (VS2013) - There was an error running the selected code generator

Stumbled upon a problem when trying to create a new empty controller. Need to investigate what is going wrong. Workaround is to create a controller by hand. :(

 

 

 

 

 

 

Passing ViewData to Html.Partial

If you want to pass more than just a model to your partial page then you can use the following.

Html.RenderPartial(
                      "NameOfThePartial"
                     , TheModel
                     , new ViewDataDictionary(this.ViewData){ { "index", index } }
                     ); 

 

 

In addition to the Viewdata which is already in the dictionary you pass in the additional index value. Hope this is of any help.

A new beginning

Started this new blog, not sure if the old content will be migrated. Aim is to collect anything related to the .Net framework and share knowledge.

Sponsored by

Calendar

<<  November 2018  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

RecentComments

TextBox