Follow us

Header Ads

FormUsingTagHelpers in Asp.Net Core

 FormUsingTagHelpers in Asp.Net Core[Without Database]

Packages

 "Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" 

Controllers[Folder]

HomeController.cs

using FormUsingTagHelpers.Models;
using Microsoft.AspNetCore.Mvc;

namespace FormUsingTagHelpers.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Index(Employee e)
        {
            return "Name=" + e.Name + "Gender=" + e.Gender + "Age=" + e.Age + "Salary=" + e.Salary + "Married=" + e.Married +;
        }
    }
}

Models[Folder]

Employee.cs

namespace FormUsingTagHelpers.Models
{
    public class Employee
    {
        public string    Name { get; set; }
        public Gender Gender { get; set; }
        public int Age { get; set; }
        public int Salary { get; set; }

        public string Married { get; set; }
    }
    public enum Gender
    {
        Male,Female
    }
}

Views[Folder]

Home

Index.cshtml

@model FormUsingTagHelpers.Models.Employee
@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>
<div class="container">
    <div class="row">

 <div class="col-sm-4 mx-auto">
     <form asp-action="Index" asp-controller="Home" method="post">
    
    <!--Name is Model Field Name-->
    <label asp-for="Name"></label>
 <input asp-for="Name" placeholder="Enter Your Name" class="form-control"/><br />
 <select asp-for="Gender" class="form-control" asp-items="Html.GetEnumSelectList<Gender>()"><br />
     <option value="">Select Gender</option>
 </select><br />
 <input asp-for="Age" placeholder="Enter Your Age" class="form-control"/><br />
 <input asp-for="Salary" placeholder="Enter Your Salary" class="form-control"/><br />
 <label>Married:</label><br/>
 <label>Yes</label>
 <input type="radio" asp-for="Married" value="yes"/>
 <label>No</label>
 <input type="radio" asp-for="Married" value="No"/>
 <br />
 <input type="submit" value="Submit" class="btn btn-primary btn-block"  />
 </form>
 </div>

</div>
</div>



Post a Comment

0 Comments