Click here to Skip to main content
12,394,769 members (73,137 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Validate Input in ASP.NET MVC

, 2 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip explains how to submit your form with HTML content in MVC.


When you develop an app, sometimes your requirements could be that you want to send HTML values from view to the controller. Sometimes, we use HTML Editors to save some information into the database. By default, ASP.NET MVC doesn't allow a user to submit the HTML content. So let's see how to submit your form with HTML content.

Using the Code

  1. Open Visual Studio, then select "New Project", then select ASP.NET MVC 4 Application.

  2. Provide a project name, then click OK.
  3. Select Internet Application, then click OK.

  4. Create a New Model.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    namespace ValidateInputDemo.Models
        public class ValidateModel
            public string description { get; set; }
  5. Add a new method to your Controller.


    public ActionResult ValidateInput()
        return View();
    public ActionResult  ValidateInput(string description)
        ValidateModel validateInputModel = new ValidateModel();
        validateInputModel.description = description;
        return View(validateInputModel);


    @model ValidateInputDemo.Models.ValidateModel
           ViewBag.Title = "ValidateInput";
    @using (@Html.BeginForm("ValidateInput","Home", 
    FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
        <label id="lblDescription">Description
         @Html.TextAreaFor(m=>m.description, new {@id="txtDescription",@name="description" })
        <input type="submit" id="bttn_Submit" />

    You can see in the code above, there is a text area and a submit button, have a look in the browser. Press F5.

    You can see in the preceding screen, if you type something into the description and press Submit then nothing happens.

    Now check the following example. Add HTML content into text area.

    You will get the error above. This error comes because this is the security from ASP.NET MVC. For applications, a user cannot send HTML values to the controller, but sometimes we want to send values to the controller.

    For resolving this issue, we have the ValidateInput(false) attribute. Just put this into your controller and have a look.

    public ActionResult  ValidateInput(string description)
       ValidateModel validateInputModel = new ValidateModel();
       validateInputModel.description = description;
       return View(validateInputModel);

    Now press F5. After filling in the HTML attribute press the submit button, you will never get an error. So when you want to work with HTML attributes in your app text area or textboxes, don't forget to use validateinpute(false) in your ActionMethod.


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


About the Author

Mishra Sourabh
Software Developer
India India
Sourabh Mishra is an IT geek, Developer, Trainer and Animator. He is a Microsoft guy, He is very passionate about Microsoft technologies and a true .Net Warrior. Sourabh started his career, when he was just 15 year old. He loves computer from his childhood.
His programming experience includes C/C++, Asp.Net, C#,, WCF, Sqlserver, Entity Framework, MVC and Jquery.

Sourabh is awarded by Most Valuable Professional Status. He is always ready to learn new technologies, sharing his knowledge to several online community forums. He loves Bond movies.

You may also be interested in...

Comments and Discussions

Questionhi Pin
mihir549018-Aug-15 20:01
membermihir549018-Aug-15 20:01 
Questionhi Pin
mihir549018-Aug-15 19:45
membermihir549018-Aug-15 19:45 
Questionalidate Input in ASP.NET MVC Pin
Bijeshpb843-Aug-14 20:42
memberBijeshpb843-Aug-14 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Apr 2014
Article Copyright 2014 by Mishra Sourabh
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid