MVC4 – How to create form to input user data?

This is very easy. Just create one model, if you have not created yet. For example like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyAuction.Models
{
    public class Auction
    {

        public long Id { get; set; }

        public string Title { get; set; }
        public string Description { get; set; }
        public string ImageUrl { get; set; }

        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public decimal StartPrice { get; set; }
        public decimal? CurrentPrice { get; set; }


    }
}

Create action for your controller:

        public ActionResult Create()
        {
            return View();
        }

Right click of your mouse inside of this action, from menu choose “Add View”
1.Check “Create strongly typed view”
2.Choose your model (if it is not visible yet, just run your application once, after that it will be visible)
3.Scaffold template: Create
4.Click “Add”

That’s it you created form to input data. It should look like this:

@model MyAuction.Models.Auction

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Auction</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ImageUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ImageUrl)
            @Html.ValidationMessageFor(model => model.ImageUrl)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.StartTime)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.StartTime)
            @Html.ValidationMessageFor(model => model.StartTime)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EndTime)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EndTime)
            @Html.ValidationMessageFor(model => model.EndTime)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.StartPrice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.StartPrice)
            @Html.ValidationMessageFor(model => model.StartPrice)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CurrentPrice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CurrentPrice)
            @Html.ValidationMessageFor(model => model.CurrentPrice)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}