Disable client side validation for hidden fields in MVC

This is something that bugged me for a while, there is also quite a bit of confusion online about how to do it.

If you need to enable and disable client side validation for fields that become visible/hidden on a form as user fills out the form it is actually quite easy to do.

Just go about hiding and showing the controls you need to but add this line of javascript after your jqueryval script bundle:

jQuery.validator.defaults.ignore = ":hidden";

Basically this will cause jquery validation to ignore hidden fields in your form, which is what you want if they are hidden.

Source: MVC unobtrusive validation – conditionally disable/enable validation

Get the identity of the inserted row

When creating stored procedures you will want the identity of the newly inserted row returned.

It’s quite simple, just use SCOPE_IDENTITY.

SQL Example:

	
CREATE PROCEDURE CreateClient
    -- Add the parameters for the stored procedure here
    @ClientName nvarchar(MAX), @ClientAcronym nvarchar(MAX)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO Clients
    VALUES(@ClientName, @ClientAcronym)

    SELECT SCOPE_INDENTITY()
END
GO

C# Example

public int Create(Client entity)
{
    using (SqlConnection connection = new SqlConnection(Settings.SQL_CONNECTION_STRING))
    {
        try
        {
            connection.Open();

            using (SqlCommand cmd = new SqlCommand("CreateClient", connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@ClientName", entity.Username));
                cmd.Parameters.Add(new SqlParameter("@ClientAcronym", entity.Email));

                var id = cmd.ExecuteScalar();

                return Convert.ToInt32(id);
            }
        }
        catch (Exception ex)
        {
            // TODO: Log error

            return -1;
        }
    }
}

Remember, ExecuteScalar is important here. It gives you back your identity.

Windows Authentication Restrict by Domain

There seems to be some confusion online in regards to how you restrict a user based on domain when using Windows Authentication.

It can be done in the config file, in my case it was my Web.Config file.

<authentication mode="Windows" />
    <authorization>
      <allow roles="DOMAIN\Domain Users"/>
      <deny users="*" />
    </authorization>

Where you see DOMAIN put in the domain you want to allow, leave the user as Domain Users.

You can do this for multiple domains, works a charm!

Source: mvc windows authentication allow all users from domain