Click here to Skip to main content
15,881,172 members
Articles / Web Development / HTML

ASP.NET MVC3 Razor With jQuery For Beginners

Rate me:
Please Sign up or sign in to vote.
4.92/5 (214 votes)
19 Apr 2012CPOL38 min read 1M   29.8K   369  
Simple tutorial to create first MVC app
This is a simple introduction tutorial that can help ASP.NET MVC3 beginners to create a first MVC application. Following the instructions and using code attached to this article, you will be able to create a simple MVC web application and extend it.
@model JQueryMVC.Models.User
@{
    ViewBag.Title = "Edit";
}
<h2>
    Edit User Details</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<form method="post" action="/User/Edit/@Model.UserID">

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>
        @Html.HiddenFor(model => model.UserID)
        <div class="editor-label">
            <label for="Name">
                Name</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Name" id="Name" value="@Model.Name" />
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            <label for="Address">
                Address</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Address" id="Address" value="@Model.Address" />
            @Html.ValidationMessageFor(model => model.Address)
        </div>
        <div class="editor-label">
            <label for="Town">
                Town</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Town" id="Town" value="@Model.Town" />
            @Html.ValidationMessageFor(model => model.Town)
        </div>
        <div class="editor-label">
            <label for="County">
                County</label>
        </div>
        <div class="editor-field">
            <input type="text" name="County" id="County" value="@Model.County" />
            @Html.ValidationMessageFor(model => model.County)
        </div>
        <div class="editor-label">
            <label for="Country">
                Country</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Country" id="Country" value="@Model.Country" />
            @Html.ValidationMessageFor(model => model.Country)
        </div>
        <div class="editor-label">
            <label for="Email">
                Email</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Email" id="Email" value="@Model.Email" />
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="editor-label">
            <label for="DoB">
                Date of birth</label>
        </div>
        <div class="editor-field">
            <input type="text" name="DoB" id="DoB" value="@Model.DoB"/>
            @Html.ValidationMessageFor(model => model.DoB)
        </div>
        <div class="editor-label">
            <label for="IsActive">
                Is Active</label>
        </div>
        <div class="editor-field">
            <input type="checkbox" name="IsActive" id="IsActive" value="@Model.IsActive" />
            @Html.ValidationMessageFor(model => model.IsActive)
        </div>
        <div class="editor-label">
            <label for="UserName">
                User name</label>
        </div>
        <div class="editor-field">
            <input type="text" name="UserName" id="UserName" value="@Model.UserName" />
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        <div class="editor-label">
            <label for="Password">
                Password</label>
        </div>
        <div class="editor-field">
            <input type="password" name="Password" id="Password" value="@Model.Password" />
            @Html.ValidationMessageFor(model => model.Password)
        </div>
        <div class="editor-label">
            <label for="Rating">
                Rating</label>
        </div>
        <div class="editor-field">
            <input type="text" name="Rating" id="Rating" value="@Model.Rating" />
            @Html.ValidationMessageFor(model => model.Rating)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</form>
<div>
    <a href="/User/Index">Back to list</a> | <a href="/User/Details/@Model.UserID">Details</a>
</div>


@section PageScripts{
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input#DoB").datepicker();
        });
    </script>
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager Microsoft
Serbia Serbia
Graduated from Faculty of Electrical Engineering, Department of Computer Techniques and Informatics, University of Belgrade, Serbia.
Currently working in Microsoft as Program Manager on SQL Server product.
Member of JQuery community - created few popular plugins (four popular JQuery DataTables add-ins and loadJSON template engine).
Interests: Web and databases, Software engineering process(estimation and standardization), mobile and business intelligence platforms.

Comments and Discussions