Archive

Archive for the ‘MVC’ Category

MVC Razor View escape character

February 13th, 2015 No comments

The Problem

You want to use your model or some code to append a variable inline with a string.
Assume you want to create a URL with a dynamic element like:

http://appserver-dev.domain.com

where -dev will be an appsetting accessed in razor from @Settings.Default.URLSuffix

When you add the @Settings value in like so;

http://appserver@Settings.Default.URLSuffix.domain.com

it treats the @Settings.Default.URLSuffix as a text string, outputting “http://appserver@Settings.Default.URLSuffix.domain.com” not “http://appserver-dev.domain.com” as you were hoping. What is the escape character?

The Solution

@@ is the escape sequence.. so that should be easy..

http://appserver@@Settings.Default.URLSuffix.domain.com

The problem now is it treats the .domain.com as part of the code and is not happy because a string object does not have .domain.com methods.

http://appserver@@Settings.Default.URLSuffix;.domain.com

Add a semi-colon to end the code and that is the problem solved.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Categories: MVC, Uncategorized Tags:

IIS Express and windows authentication

February 13th, 2015 No comments

How to control the IIS Express Authentication settings

To set the windows authentication parameters for you MVC applicaiton there are two ways to do this .

  1. On a per project basis, by going to the properties of the web app, F4, and setting the windows authentication to on or off; or
  2. Globally for the entire computer.
    • go to the following path, %HemePath%\Documents\IISExpress\config
    • edit the “applicationhost.config” file
    • find the line
    • Change the value as required and then save
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Categories: MVC, Uncategorized Tags:

Use CKEditor on MVC 3 Razor Website

December 6th, 2011 12 comments

This post provides the basic steps the implement a CKEditor control into your MVC Razor web page.

This post assumes you have a working MVC website, with a standard _Layout.cshtml page.

Follow these easy steps:

  1. Download the latest version of the CKEditor from here
    You only need to download the basic package.

    At the time of writing I downloaded the zip file of CKEditor 3.6.2

    You will see there is an ASP.net control that can be downloaded but this is not required to make it work with MVC.

  2. Unzip the downloaded package
  3. In windows explorer copy the “ckeditor” folder into the root folder of your website.
  4. In Visual studio and click the “Show All Files” icon.
  5. right-click on the ckeditor folder and choose include in project, this can take a little while..
  6. In the <head> of your _Layout.cshtml file reference the ckeditor.js file
    <script src="@Url.Content("~/ckeditor/ckeditor.js")" type="text/javascript"></script>
    
  7. Create a model class with a property you wish to bind to your HTML editor
  8. Create a strongly-typed view (use scaffold template of edit if you like)
  9. Now the easy bit, the way this works is by generating a <textarea> control with the class of “ckeditor” and an id, so the html for this would look like the following. Note the id can be anything you want.
    <textarea class="ckeditor" id="text-details" name="Details">some text</textarea>
    

    To generate this in MVC use the following

    @Html.TextAreaFor(model => model.Details, new { @class="ckeditor", @id="text-details"})
    

    where model.Details is the property of the strongly-typed class you passed through

That’s it. Run the site and visit the page. Your text area control will be rendered as the CKEditor control.

Happy editing

VN:F [1.9.22_1171]
Rating: 8.9/10 (21 votes cast)

Categories: MVC Tags:

How to run MvcMusicStore v3.0 with SQL Server Express

November 11th, 2011 4 comments

I’ve been trying to do the MVCMusicStore v3.0b tutorial, and I wanted to use SQLExpress rather than SqlServerCe.4.0.
When trying to do this I ran into a number of problems but I finally figured it out.

This guide assumes you already have a working copy of SQL Server Express and that your user account is a System Administrator for the database.

This is the link to the PDF document for the v3.0b tutorial. MvcMusicStore v3.0

(*all page numbers quoted as on the document, page 45 is actually Page 46 when listed in Acrobat)

Below is what I did and the 2 solutions that I found..

  1. As on page 45* I created the AppData Folder.
  2. I downloaded the MVC assets and extracted the files from the zip file.
  3. Right click on AppData and add an existing file, and add in the MvcMusicStore.mdf.
  4. Edit the web.config and added the following connection string:
    <connectionStrings>
      <add name="MusicStoreDb"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MvcMusicStore.mdf;Integrated Security=True;"
        providerName="System.Data.SqlClient" />
    </connectionStrings>
    	
  5. Added the MusicStoreEntities.cs as on page 47.

At this point Debug the site and I was getting errors. First I was getting errors about meta data failures.
I managed to play a bit more and somehow fixed the meta data issues so that it was no longer erroring, but the queries were not returning any data.

Then I noticed the following 2 issues:

  1. Write some code like the following
    			MusicStoreEntities db = new MusicStoreEntities();
    			string connection = db.Database.Connection.ConnectionString;
    		

    Run up in debug and check the value stored in the connection string.
    Note that the database name is related to the application MvcMusicStore, “MvcMusicStore.Models.MusicStoreEntities”? WTF?

  2. Put a watch on the db.Database.Genres object and note the sql statement is is going to execute. The table name is “dbo.Genres”.
    The issue? Your datbase doesn’t have a “Genres” table it has a “Genre” table (minus the ‘s’).

Solution 1

So to fix these issues I have done the following:

  1. Create a default constructor in the MvcMusicStoreEntities class like follows:

    public MusicStoreEntities()
    {
        this.Database.Connection.ConnectionString = 
               ConfigurationManager.ConnectionStrings["MusicStoreDb"].ConnectionString;
    }
    

    This over-rides the connection string with the one that we have specified in the web.config.

  2. Pluralise your database.

    If your hosting your database on SQL Server you can use the SQL Management studio, or if you are hosting it in AppData then use the server explorer.
    Right click on each table and select “Open Table Definition”. Press F4 to view the properties window, Edit the “Name” property and change the name of the table and add an ‘s’ to the end, Genre -> Genres, Album -> Albums, Artist -> Artists etc. Save the file to apply the name change.

  3. Edit: A bit more digging and I also found this solution to the Pluralised problem, this removes the pluralised table names. Probably a bit easier than the above solution
    In your MusicStoreEntities class add the following

            using System.Data.Entity.ModelConfiguration.Conventions;
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            }
    
  4. Debug your site and it should all work now.

Solution 2

In attempting to work out the above solution I also found a second solution.

  1. Ensure the constructor is removed from the MusicStoreEntities class.
  2. Edit the web.config file and add the following connection string

    		<connectionStrings>
    			<add name="MusicStoreDb"
    			  connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;"
    			  providerName="System.Data.SqlClient" />
    		</connectionStrings>
    		
  3. Add the Sample Data as on page 47 & 48 of the PDF guide.
  4. Debug your site and it should work.

    Launch SQL Mangement Studio and refresh the list of databases, you will see you new database [MvcMusicStore.Models.MusicStoreEntities] and look at the tables and they have pluralised names.

Hope this works for you as it did for me. Happy MVC’ing.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Categories: c#, MVC Tags: