[ C#] MVC 2 101

Create New Project

In visual studio, File > New Project
`Visual C# Projects` and in the Templates pane, select `ASP.NET MVC 2 Web Application`.

The files will be generated.

SFA.sln
SFA\App_Data
SFA\bin
SFA\Content\Site.css
SFA\Controllers\AccountController.cs
SFA\Controllers\HomeController.cs
SFA\Global.asax
SFA\Global.asax.cs
SFA\Models\AccountModels.cs
SFA\obj\Debug
SFA\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
SFA\obj\Debug\Package
SFA\obj\Debug\TempPE
SFA\Properties\AssemblyInfo.cs
SFA\Scripts\jquery-1.4.1-vsdoc.js
SFA\Scripts\jquery-1.4.1.js
SFA\Scripts\jquery-1.4.1.min.js
SFA\Scripts\jquery.validate-vsdoc.js
SFA\Scripts\jquery.validate.js
SFA\Scripts\jquery.validate.min.js
SFA\Scripts\MicrosoftAjax.debug.js
SFA\Scripts\MicrosoftAjax.js
SFA\Scripts\MicrosoftMvcAjax.debug.js
SFA\Scripts\MicrosoftMvcAjax.js
SFA\Scripts\MicrosoftMvcValidation.debug.js
SFA\Scripts\MicrosoftMvcValidation.js
SFA\SFA.csproj
SFA\SFA.csproj.user
SFA\SFA.Publish.xml
SFA\Views\Account
SFA\Views\Home
SFA\Views\Shared
SFA\Views\Web.config
SFA\Views\Account\ChangePassword.aspx
SFA\Views\Account\ChangePasswordSuccess.aspx
SFA\Views\Account\LogOn.aspx
SFA\Views\Account\Register.aspx
SFA\Views\Home\About.aspx
SFA\Views\Home\Index.aspx
SFA\Views\Shared\Error.aspx
SFA\Views\Shared\LogOnUserControl.ascx
SFA\Views\Shared\Site.Master
SFA\Web.config
SFA\Web.Debug.config
SFA\Web.Release.config
----------------------------------------------------------------- UnitTest
SFA.Tests\App.config
SFA.Tests\bin
SFA.Tests\Controllers
SFA.Tests\obj
SFA.Tests\Properties
SFA.Tests\SFA.Tests.csproj
SFA.Tests\bin\Debug
SFA.Tests\Controllers\AccountControllerTest.cs
SFA.Tests\Controllers\HomeControllerTest.cs
SFA.Tests\obj\Debug
SFA.Tests\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
SFA.Tests\obj\Debug\TempPE
SFA.Tests\Properties\AssemblyInfo.cs

Build and Deploy

In Solution Manager, Right Click at the project > Build

Click `Start Debugging (F5)` icon

The default route

Global.asax.cs

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

i.e. http://localhost:52386/Home/About calls method About()

in SFA\Controllers\HomeController.cs

 

Passing The data from controller to view

Controller

ViewData["Message"] = "Welcome to ASP.NET MVC!";

View

<%: ViewData["Message"] %>

Action Anchor

View

<%: Html.ActionLink("Register", "Register") %>

Form

View : Account/LogOn.aspx

NOTE : submit to Account/LogOn.aspx

<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>

<div>
<%: Html.LabelFor(m => m.UserName) %>
</div>
<div>
<%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>

<div>
<%: Html.LabelFor(m => m.Password) %>
</div>
<div>
<%: Html.PasswordFor(m => m.Password) %>
<%: Html.ValidationMessageFor(m => m.Password) %>
</div>

<div>
<%: Html.CheckBoxFor(m => m.RememberMe) %>
<%: Html.LabelFor(m => m.RememberMe) %>
</div>

<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>

Model : AccountModels.cs

#region Models

public class LogOnModel
{
[Required]
[DisplayName("User name")]
public string UserName { get; set; }

[Required]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }

[DisplayName("Remember me?")]
public bool RememberMe { get; set; }
}

#endregion

Properties Message

Web.config

i.e. minRequiredPasswordLength

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s