ASP .NET Core Coding Standard

Jan 28 2020 12:34 PM
I am beginner in ASP.Net Core. I got the following way to develop. Can anybody please check my code for Dropdown, Logged In user checking and View? And let me know if I am doing this with a Standard way?
 
 Controller :
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using test.Data;
using test.Models;

namespace test.Controllers
{
    public class JobsController : Controller
    {
        private readonly testContext _context;

        public JobsController(testContext context)
        {
            _context = context;
        }

        // GET: Jobs
        public async Task<IActionResult> Index()
        {
            var testContext = _context.Jobs.Include(j => j.Clients).Include(j => j.Locations);
            return View(await testContext.ToListAsync());
        }

        // GET: Jobs/Details/5
        public async Task<IActionResult> Details(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var jobs = await _context.Jobs
                .Include(j => j.Clients)
                .Include(j => j.Locations)
                .FirstOrDefaultAsync(m => m.JobID == id);
            if (jobs == null)
            {
                return NotFound();
            }

            return View(jobs);
        }

        // GET: Jobs/Create
        public IActionResult Create()
        {
            ViewData["ClientID"] = new SelectList(_context.Clients, "ClientID", "Address");
            ViewData["LocationID"] = new SelectList(_context.Locations, "LocationID", "LocationID");
            return View();
        }

        // POST: Jobs/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("JobID,LocationID,ClientID,ClientContactName,ClientEmail,JobDescription,JobNo,JobCode,CreatedBy,CreatedOn")] Jobs jobs)
        {
            if (ModelState.IsValid)
            {
                _context.Add(jobs);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["ClientID"] = new SelectList(_context.Clients, "ClientID", "Address", jobs.ClientID);
            ViewData["LocationID"] = new SelectList(_context.Locations, "LocationID", "LocationID", jobs.LocationID);
            return View(jobs);
        }

        // GET: Jobs/Edit/5
        public async Task<IActionResult> Edit(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var jobs = await _context.Jobs.FindAsync(id);
            if (jobs == null)
            {
                return NotFound();
            }
            ViewData["ClientID"] = new SelectList(_context.Clients, "ClientID", "Address", jobs.ClientID);
            ViewData["LocationID"] = new SelectList(_context.Locations, "LocationID", "LocationID", jobs.LocationID);
            return View(jobs);
        }

        // POST: Jobs/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(long id, [Bind("JobID,LocationID,ClientID,ClientContactName,ClientEmail,JobDescription,JobNo,JobCode,CreatedBy,CreatedOn")] Jobs jobs)
        {
            if (id != jobs.JobID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(jobs);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!JobsExists(jobs.JobID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["ClientID"] = new SelectList(_context.Clients, "ClientID", "Address", jobs.ClientID);
            ViewData["LocationID"] = new SelectList(_context.Locations, "LocationID", "LocationID", jobs.LocationID);
            return View(jobs);
        }

        // GET: Jobs/Delete/5
        public async Task<IActionResult> Delete(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var jobs = await _context.Jobs
                .Include(j => j.Clients)
                .Include(j => j.Locations)
                .FirstOrDefaultAsync(m => m.JobID == id);
            if (jobs == null)
            {
                return NotFound();
            }

            return View(jobs);
        }

        // POST: Jobs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(long id)
        {
            var jobs = await _context.Jobs.FindAsync(id);
            _context.Jobs.Remove(jobs);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool JobsExists(long id)
        {
            return _context.Jobs.Any(e => e.JobID == id);
        }
    }
}
View :
 
Create:
 
@model test.Models.Jobs

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Jobs</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="LocationID" class="control-label"></label>
                <select asp-for="LocationID" class ="form-control" asp-items="ViewBag.LocationID"></select>
            </div>
            <div class="form-group">
                <label asp-for="ClientID" class="control-label"></label>
                <select asp-for="ClientID" class ="form-control" asp-items="ViewBag.ClientID"></select>
            </div>
            <div class="form-group">
                <label asp-for="ClientContactName" class="control-label"></label>
                <input asp-for="ClientContactName" class="form-control" />
                <span asp-validation-for="ClientContactName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ClientEmail" class="control-label"></label>
                <input asp-for="ClientEmail" class="form-control" />
                <span asp-validation-for="ClientEmail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobDescription" class="control-label"></label>
                <textarea asp-for="JobDescription" class="form-control"></textarea>
                <span asp-validation-for="JobDescription" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobNo" class="control-label"></label>
                <input asp-for="JobNo" class="form-control" />
                <span asp-validation-for="JobNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobCode" class="control-label"></label>
                <input asp-for="JobCode" class="form-control" />
                <span asp-validation-for="JobCode" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="CreatedBy" class="control-label"></label>
                <input asp-for="CreatedBy" class="form-control" />
                <span asp-validation-for="CreatedBy" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="CreatedOn" class="control-label"></label>
                <input asp-for="CreatedOn" class="form-control" />
                <span asp-validation-for="CreatedOn" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
 
 
 
Edit:
 
@model test.Models.Jobs

@{
    ViewData["Title"] = "Edit";
}

<h1>Edit</h1>

<h4>Jobs</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="JobID" />
            <div class="form-group">
                <label asp-for="LocationID" class="control-label"></label>
                <select asp-for="LocationID" class="form-control" asp-items="ViewBag.LocationID"></select>
                <span asp-validation-for="LocationID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ClientID" class="control-label"></label>
                <select asp-for="ClientID" class="form-control" asp-items="ViewBag.ClientID"></select>
                <span asp-validation-for="ClientID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ClientContactName" class="control-label"></label>
                <input asp-for="ClientContactName" class="form-control" />
                <span asp-validation-for="ClientContactName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ClientEmail" class="control-label"></label>
                <input asp-for="ClientEmail" class="form-control" />
                <span asp-validation-for="ClientEmail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobDescription" class="control-label"></label>
                <textarea asp-for="JobDescription" class="form-control"></textarea>
                <span asp-validation-for="JobDescription" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobNo" class="control-label"></label>
                <input asp-for="JobNo" class="form-control" />
                <span asp-validation-for="JobNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="JobCode" class="control-label"></label>
                <input asp-for="JobCode" class="form-control" />
                <span asp-validation-for="JobCode" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="CreatedBy" class="control-label"></label>
                <input asp-for="CreatedBy" class="form-control" />
                <span asp-validation-for="CreatedBy" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="CreatedOn" class="control-label"></label>
                <input asp-for="CreatedOn" class="form-control" />
                <span asp-validation-for="CreatedOn" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
 
Model:
 
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace test.Models
{
    [Table("Jobs")]
    public class Jobs
    {
        [Key]
        public long JobID { get; set; }

        [Required(ErrorMessage = "Please select location")]
        [Display(Name = "Location Name")]
        public Guid LocationID { get; set; }

        [Required(ErrorMessage = "Please select client")]
        [Display(Name = "Client Name")]
        public long ClientID { get; set; }

        [Display(Name = "Client Contact Name")]
        [Required(ErrorMessage = "Please enter client contact name")]
        [MaxLength(50, ErrorMessage = "Please enter client contact name less than {1}")]
        [RegularExpression(@"^\s*[A-z]+[\s[A-z]*]*$", ErrorMessage = "Please enter character only")]
        public string ClientContactName { get; set; }

        [Required(ErrorMessage = "Please enter client email")]
        [EmailAddress(ErrorMessage = "Please enter valid email")]
        [Display(Name = "Client Email")]
        [MaxLength(50, ErrorMessage = "Please enter client email less than {1}")]
        public string ClientEmail { get; set; }

        [Display(Name = "Job Description")]
        [Required(ErrorMessage = "Please enter job description")]
        [DataType(DataType.MultilineText)]
        [MaxLength(2000, ErrorMessage = "Please enter job description less than {1}")]
        public string JobDescription { get; set; }

        [Display(Name = "Job Number")]
        public long JobNo { get; set; }

        [Display(Name = "Job Code")]
        public string JobCode { get; set; }

        [Display(Name = "Created By")]
        public Guid CreatedBy { get; set; }

        [Display(Name = "Created On")]
        public DateTime CreatedOn { get; set; }

        [ForeignKey("ClientID")]
        public virtual Clients Clients { get; set; }

        [ForeignKey("LocationID")]
        public virtual Locations Locations { get; set; }

    }
}
 

Answers (1)