Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free

Asp Core Mvc: Create Form



This example shows a Create Form with Asp.Net Core Mvc.

The form creates a new entry under the Notes table.

The ID is automatically generated while the input is validated with a validation if a note title has been entered.



Create form

The Controller Action Notes / Create calls the appropriate view /Views/Notes/Create.cshtml.

The form only requires a title entry and a button: Create.



Returns the list result

After executing Create, the result is displayed in the list / Notes / Index_all



If nothing is entered in the title field, then the validation message that the necessary entries are missing is sent before sending.

The client-side check is done purely by adding the Required attribute in the data model.



public string Title { get; set; }




Empty String Server-side ModelState.IsValid

If an empty field "" is entered, the error is intercepted by the server

With ModelState.IsValid = false

The breakpoint shows that the Root / ChildNotes: Subkey: Title has ValidationState = Invalid.


Controller Create Action Method

File: Controllers / NotesController.cs


Controller: Notes

        // POST: Note/Create

        // To protect from overposting attacks, please enable the specific properties you want to bind to, for



        public async Task<IActionResult> Create([Bind("Title,Text")] NoteModel noteModel)


            //-------------< Create >-------------

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await Userinfos.UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--


            if (ModelState.IsValid)


                //---< Add Record >---

                //< autofill >

                noteModel.DtCreated = DateTime.Now;

                noteModel.DtEdit = DateTime.Now;

                noteModel.IDUser = IDUser;

                noteModel.sumViews = 0;

                //</ autofill >


                //-< add >-

                //*to buffer


                //*to server

                await _dbContext.SaveChangesAsync();

                //-</ add >-


                //-< to List >

                return RedirectToAction(nameof(Index_All));

                //-</ to List >

                //---</ Add Record >---




                //--< errors >--

                //*validation fails, no Title

                //*with Errors back to create form

                return View( noteModel);

                //--</ errors >--


            //-------------</ Create >-------------



Data Model: NoteModel.

The model has a [Required] attribute to check validity before the variable Title.

This validation attribute ensures that at least one real character must be entered in the title during the new creation.


File to the model

Models / NoteModel.cs




Data Model NoteModel class

Note Model

using System;

//< using >

using System.ComponentModel.DataAnnotations;

//</ using >


namespace IWois_Core.Models


    public class NoteModel


        //-------------< Class: Model_Project >-------------

        #region Variables

        //------------< Region: Variables >------------



        public long IDNote { get; set; }      

        public long IDUser { get; set; }




        public string Title { get; set; }

        public string Text { get; set; }


        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]

        public DateTime? DtEdit { get; set; }


        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]

        public DateTime? DtCreated { get; set; }

        public int? sumViews { get; set; }


        //------------</ Region: Variables >------------

        #endregion /Variables

        //-------------</ Class: Model_Project >-------------






View: Create

File: /Views/Notes/Create.cshtml

In the Razor code, the IDUser field was commented out in this case.

You could also insert the field as Type = Hidden. However, it is not used in this case.

@model IWois_Core.Models.NoteModel



    ViewData["Title"] = "Create";

    Layout = "~/Views/Shared/_Layout.cshtml";



<hr />

<div class="row">

    <div class="col-md-4">

        <form asp-action="Create">

            <div class="form-group">

                <input type="submit" value="Create" class="btn btn-default" />


            <div asp-validation-summary="ModelOnly" class="text-danger"></div>


            @*Visible Asp Core Field*@

            @*<div class="form-group">

                <input asp-for="IDUser" class="form-control" disabled />

                <span asp-validation-for="IDUser" class="text-danger"></span>



            @*Hidden Asp Core Field*@

            @*<input type="hidden" asp-for="IDUser"  />*@



            <div class="form-group">

                <label asp-for="Title" class="control-label"></label>

                <input asp-for="Title" class="form-control" />

                <span asp-validation-for="Title" class="text-danger"></span>


            <div class="form-group">

                <label asp-for="Text" class="control-label"></label>

                <input asp-for="Text" class="form-control" />

                <span asp-validation-for="Text" class="text-danger"></span>








@section scripts {

    @{await Html.RenderPartialAsync("_ValidationscriptsPartial");}




Asp.Net Core MVC: Form Get field values without model
Asp MVC: Large text box Input, Textarea, Div with HTML
Asp Core Mvc: Example of a Create Form
Asp Core: hide input field, hide ID field


Contact for Jobs, Project Requests: