Dynamically changing HTML form action using jQuery

In order to test a RESTful service I’m currently working on I’ve created a simple HTML page with a bunch of forms. These forms generate various POST/GET requests to my REST service API methods, so I can manually test the service during a development process. One issue I faced was that the action attribute of HTML form element is static while I need to modify it dynamically based on user’s input (i.e. so I can send a GET requests to a URL like http://www.myrestfulservice.com/item/{itemID} where itemID is entered to the form by a user). JavaScript comes to mind.

So here is how this could easily be done with jQuery:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js'></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#getForm :submit").click(function() {
                var item = $(":input#itemId").val();
                $("#getForm").attr("action", "http://www.myrestfulservice.com/" + item);
            });
        });
    </script>
</head>
<body>
    <p>
        Enter item ID get:
        <input type="text" size="50" id="itemId" />
    </p>
    <form id="getForm" method="get" action="">
        <input type="submit" value="Get Item" />
    </form>
</body>
</html>

Getting Database column length with LINQ to Entities

During the work with ADO.Net Entity Framework sometimes you need to know a metadata for a database you’re working with such as a nchar/nvarchar column max length. Trying to update a DB column using LINQ to Entities with a string greater than the column max length leads to an SqlException (e.g. System.Data.SqlClient.SqlException: String or binary data would be truncated); therefore you need to check that your data does not exceed DB column boundaries manually. LINQ won't do this for you on a run-time.

If you don't want to have all your Entity Model metadata hardcoded, you need to get it on a run-time. Here is described how you can do this with LINQ to SQL.

Unfortunately this approach seems not to work for Entities Model. In order to do this for Entities you would need to query MetadataWorkSpace class and get a conceptual (CSpace) metadata for your Entity:

public static int GetMaxLength(Type entityType, string columnName)
{
    int result = 0;

    using (myEntities context = new myEntities())
    {
        var queryResult = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace)    //getting conceptual entity metadata
                          where meta.BuiltInTypeKind == BuiltInTypeKind.EntityType
                                && (meta as EntityType).Name == entityType.Name    //selecting desired entity by name
                          from property in (meta as EntityType).Properties
                          where property.Name == columnName    //selecting an entity property that matches the column name
                                && (property.TypeUsage.EdmType.Name == "String")            
                          select property.TypeUsage.Facets["MaxLength"].Value;              

        if (queryResult.Count() > 0)
        {
            result = Convert.ToInt32(queryResult.First());
        }
    }
    return result;
}

When you call this method, just pass a type of your entity e.g.

GetMaxLength(typeof(myEntity), "myColumnName");