Business ERP Part 31 Create Financial Years in ASP.NET MVC

Business ERP Part 31 Create Financial Years in ASP.NET MVC

Hi, Dear's here we learn how to implement Auto Time Table Generator in Visual Studio using C# Windows Form. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials


In this part we are going to Create Financial Year's in tblFinancialYearTable, so now follow below code.

Source Code: 
Action C# Code :
using DatabaseLayer;
using ERP_App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ERP_App.Controllers
{
    public class AdminConfigController : Controller
    {
        private BusinessERPDbEntities DB = new BusinessERPDbEntities();

public ActionResult CreateFinancialYear()
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
            {
                return RedirectToAction("Login", "Home");
            }
            var userid = 0;
            var usertypeid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
            if (usertypeid != 1)
            {
                return RedirectToAction("Admin", "Dashboard");
            }
            var finacialyearmv = new FinancialYearMV();
            finacialyearmv.UserID = userid;
            finacialyearmv.StartDate = DateTime.Now;
            finacialyearmv.EndDate = DateTime.Now.AddDays(365);
            return View(finacialyearmv);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateFinancialYear(FinancialYearMV finacialyearmv)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
            {
                return RedirectToAction("Login", "Home");
            }
            var userid = 0;
            var usertypeid = 0;
            int.TryParse(Convert.ToString(Session["UserID"]), out userid);
            int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
            if (usertypeid != 1)
            {
                return RedirectToAction("Admin", "Dashboard");
            }
            if (ModelState.IsValid)
            {
                var checkfinancialyear = DB.tblFinancialYears.Where(u => u.FinancialYear == finacialyearmv.FinancialYear.Trim()).FirstOrDefault();
                if (checkfinancialyear == null)
                {
                    var newfinancialyear = new tblFinancialYear();
                    newfinancialyear.FinancialYear = finacialyearmv.FinancialYear;
                    newfinancialyear.StartDate = finacialyearmv.StartDate;
                    newfinancialyear.EndDate = finacialyearmv.EndDate;
                    newfinancialyear.IsActive = finacialyearmv.IsActive;
                    newfinancialyear.UserID = userid;
                    DB.tblFinancialYears.Add(newfinancialyear);
                    DB.SaveChanges();
                    return RedirectToAction("AllFinanicalYears");
                }
                else
                {
                    ModelState.AddModelError("FinancialYear", "Already Exist!");
                }
            }
            return View(finacialyearmv);
        }
    }
}
View Code :
@model ERP_App.Models.FinancialYearMV

@{
    ViewBag.Title = "Financial Year";
}

<div class="col-lg-6">
    <div class="card card-default mb-6">
        <div class="card-header">New Financial Year</div>
        <div class="card-body">
            @using (Html.BeginForm("CreateFinancialYear", "AdminConfig", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                <div class="form-horizontal">
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    @Html.HiddenFor(u => u.FinancialYearID)
                    @Html.HiddenFor(u => u.CreatedBy)
                    @Html.HiddenFor(u => u.UserID)

                    <div class="form-group">
                        @Html.LabelFor(model => model.FinancialYear, htmlAttributes: new { @class = "control-label col-md-6" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.FinancialYear, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.FinancialYear, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-6" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-6" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-6" })
                        <div class="col-md-10">
                            <div class="checkbox">
                                @Html.EditorFor(model => model.IsActive)
                                @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create" class="btn btn-success" />
                            @Html.ActionLink("Back", "AllFinanicalYears", null, new { @class = "btn btn-outline-default" })
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>

Comments