Introduction

This article describes how to allow a HTML Element in a Web API form. Now see how to create the application.

Step 1

Create a Web API application

Step 2

Create the Model Class as in the following:

Add the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace HtmlAPI.Models
  7. {
  8. public class ModelClass
  9. {
  10. [AllowHtml] public String Topic { get; set; }
  11. [AllowHtml] public String Doc { get; set; }
  12. }
  13. }

Step 3

In the HomeController write the following code:

Add the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using HtmlAPI.Models;
  7. namespace HtmlAPI.Controllers
  8. {
  9. public class HomeController : Controller
  10. {
  11. [HttpGet]
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. [HttpPost]
  17. public ActionResult GetData(ModelClass obj)
  18. {
  19. if (ModelState.IsValid)
  20. {
  21. string Top = obj.Topic;
  22. string Con = obj.Doc;
  23. return Content("Topic =" + Top + "Doc= :-" + Con);
  24. }
  25. else
  26. return Content("It is Invalide");
  27. }
  28. }
  29. }

Step 4

In the view add the following code:

Add the following code:

  1. @model HtmlAPI.Models.ModelClass
  2. @{
  3. Layout = null;
  4. }
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <title>Index</title>
  9. </head>
  10. <body>
  11. <div>
  12. @using (Html.BeginForm("GetData", "Home", FormMethod.Post))
  13. {
  14. @Html.LabelFor(p=>p.Topic,"Topic")
  15. @Html.TextBoxFor(p=> p.Topic)
  16. @Html.LabelFor(p=>p.Doc)
  17. @Html.TextBoxFor(p => p.Doc, "Doc");
  18. <input type="submit" name="Submit" value="submit" />
  19. }
  20. </div>
  21. </body>
  22. </html>

Step 5

Execute the application:

h5.jpg

h6.jpg

h7.jpg