User can add and delete customers I am using asp.net core entitiy framework.  The page displays the customers.  The user can add new customers or delete existing ones.  I am looking for ways to improve my code.

Here is my cshtml page

@Html.Partial("Index")
@model ecommerce.Models.Users
&lt;h6&gt;Add a new customer&lt;/h6&gt;
&lt;form asp-controller="Customer" asp-action="AddCustomer" method="post" role="form"&gt; 
        &lt;span asp-validation-for="name"&gt;&lt;/span&gt;
        &lt;label asp-for="name"&gt;Name&lt;/label&gt;
        &lt;input asp-for="name"/&gt;
        &lt;button type="submit"&gt;Add&lt;/button&gt;
        &lt;input asp-for="created_at" value="@DateTime.Now" class="time"/&gt;
        &lt;input asp-for="updated_at" value="@DateTime.Now" class="time"/&gt;
&lt;/form&gt;
@{
        if(ViewBag.NotUnique!=null)
        {
                &lt;p&gt;Name is alread in DB&lt;/p&gt;
        }

        if(ViewBag.Empty!=null)
        {
                &lt;p&gt;Name is empty&lt;/p&gt;
        }
        &lt;table&gt; 
                &lt;tr&gt;
                        &lt;th&gt;Customer Name&lt;/th&gt;
                        &lt;th&gt;Created Date&lt;/th&gt;
                        &lt;th&gt;Actions&lt;/th&gt;
                &lt;/tr&gt;
        @if(ViewBag.Users != null)
        {
                foreach(var user in ViewBag.Users)
                {
                        &lt;tr&gt;
                                &lt;td&gt;@user.name&lt;/td&gt;
                                &lt;td&gt;@user.created_at.ToString("MMM")  @user.created_at.ToString("dd") @user.created_at.ToString("yyyy")&lt;/td&gt;

                                  &lt;form action="/DeleteCustomer/@user.id" method="post"&gt;
                                      &lt;td&gt;  &lt;input type="submit" name="submit" value="Delete"/&gt;&lt;/td&gt;
                                  &lt;/form&gt; 

                        &lt;/tr&gt;



                }
        }
         &lt;/table&gt;
}


Here is my cs file

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ecommerce.Models;
using ecommerce.Controllers;
using System.Linq;

namespace ecommerce.Controllers
{
    public class CustomerController : Controller
    {
        // GET: /Home/

        private YourContext _context;

        public CustomerController(YourContext context)
        {
            _context = context;//connection for my db
        }

        [HttpGet]
        [Route("customers")]
        public IActionResult Customers()
        {
            ViewBag.Users = _context.Users.AsEnumerable();
            return View();
        }

        [HttpPost]
        [Route("deleteCustomer/{id}")]
        public IActionResult DeleteCustomer(int id)
        {
            var temp = _context.Users.Where(x =&gt; x.id == id).SingleOrDefault();
            _context.Users.Remove(temp);
            _context.SaveChanges();
            return RedirectToAction("Customers");
        }

        [HttpPost]
        [Route("addCustomer")]
         public IActionResult AddCustomer(String name, DateTime created_at)
        {
            Users user = new Users();
            user.name = name;
            user.created_at = created_at;
            user.updated_at = created_at;

            var temp = _context.Users.AsEnumerable().ToList();
            if(temp.Any(x =&gt; x.name == name))
            {
                ViewBag.NotUnique = true;
                return RedirectToAction("Customers");
            }
            if (user.name != null)
            {
                // Success

                _context.Users.Add(user);
                _context.SaveChanges();
                return RedirectToAction("Customers");
            }

            ViewBag.Empty = true;
            // If there were errors, re-render the view but bind the model that was submitted.
            return RedirectToAction("Customers");
        }
    }
}
You don't need to enumerate twice here    


var temp = _context.Users.AsEnumerable().ToList();



You can just call .ToList().
SingleOrDefault() also accepts predicates so the first .Where() call is unnecessary, you can just do it like this : 

Your code :        


var temp = _context.Users.Where(x =&gt; x.id == id).SingleOrDefault();



Can become :

var temp = _context.Users.SingleOrDefault(x =&gt; x.id == id);

I don't see any reason to call .ToList() here :


    var temp = _context.Users.ToList();
    if (temp.Any(x =&gt; x.name == name))
    {
        ViewBag.NotUnique = true;
        return RedirectToAction("Customers");
    }



You are not performing any List&lt;T&gt; specific operations, (I assume Users is  IEnumerable&lt;T&gt;), you are good with a pure IEnumerable&lt;T&gt; here. In fact, you don't even need the temp variable, you can just directly access _context.Users.
You can have just ActionResult instead of IActionResult as a return type for your methods.


Design


Usually asp.net-mvc projects work with multiple DTO's (Data Transfer Object), which are almost identical to your actual model, but you can remove some things and add others, so that you can define the way your data will be passed around your application. Often the ViewModel is mapped in a way to it's respective DTO, to ease the creation of both objects.

Even tho simple projects probably wont need any additional DTO's.
Your models should have  proper validation instead of some check against null in your controller, which should only verify that the validation is successful. For example DataAnnotations offers quite some variety of pre-written validation attributes. You can of course extend this with your own, but it already has the core things you might need. You can find examples here