Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

removing an item from a list

$
0
0

I've got a page where the user can type into a text box and click the add button, then what is added will show up in the list box to the side. I also need to be able to remove items from that list. What would be the best and most user friendly way of setting this up? 

Controller:

public class SpecificationsController : Controller
    {
       public IActionResult SpecsDisplay(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();

            model.AssetTypes = context.Asset_Type.Where(a => a.IsActive).ToList();
            model.Conditions = context.Condition.Where(c => c.IsActive).ToList();
            model.Manufactures = context.Manufacture.ToList();
            model.Priorities = context.Priority.Where(p => p.IsActive).ToList();
            model.Statuses = context.Status.Where(s => s.IsActive).ToList();
            model.TicketTypes = context.TicketType.Where(t => t.IsActive).ToList();
            model.Vendors = context.Vendor.ToList();

            return View(model);
        }
        [HttpPost]
       public IActionResult UpdateAssetType(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Asset_Type.Add(model.AssetType);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdateCondition(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Condition.Add(model.Condition);
            var result = context.SaveChanges();


            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdatePriority(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Priority.Add(model.Priority);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdateStatus(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Status.Add(model.Status);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdateTicketType(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.TicketType.Add(model.TicketType);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdateManufacture(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Manufacture.Add(model.Manufacture);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }

        [HttpPost]
        public IActionResult UpdateVendor(SpecificationsViewModel model)
        {
            var context = new MaintenanceContext();
            context.Vendor.Add(model.Vendor);
            var result = context.SaveChanges();

            return RedirectToAction("SpecsDisplay", "Specifications");
        }



    }

ViewModel:

 public class SpecificationsViewModel : AutoMapViewModel<Asset>
    {
        public Asset_Type AssetType { get; set; }
        public Manufacture Manufacture { get; set; }
        public Vendor Vendor { get; set; }
        public Condition Condition { get; set; }
        public Status Status { get; set; }
        public TicketType TicketType { get; set; }
        public Priority Priority { get; set; }


        public List<Priority> Priorities { get; set; }
        public List<TicketType> TicketTypes { get; set; }
        public List<Asset_Type> AssetTypes { get; set; }
        public List<Manufacture> Manufactures { get; set; }
        public List<Vendor> Vendors { get; set; }
        public List<Status> Statuses { get; set; }
        public List<Condition> Conditions { get; set; }
    }

View:

@using Maintenance.Web.Models.Specs
@model SpecificationsViewModel<div>
    @using (Html.BeginForm("UpdateAssetType", "Specifications"))
    {
       @Html.TextBoxFor(m => m.AssetType.Type) <input type="submit" value="Add" />

       @Html.ListBoxFor(m => m.AssetTypes, new SelectList(Model.AssetTypes, "Id", "Type"))
    }
    @using (Html.BeginForm("UpdateCondition", "Specifications"))
    {
        @Html.TextBoxFor(m => m.Condition.Condition1) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.Conditions, new SelectList(Model.Conditions, "Id", "Condition1"))
    }
    @using (Html.BeginForm("UpdatePriority", "Specifications"))
    {
        @Html.TextBoxFor(m => m.Priority.PriorityType) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.Priorities, new SelectList(Model.Priorities, "Id", "PriorityType"))
    }
    @using (Html.BeginForm("UpdateStatus", "Specifications"))
    {
        @Html.TextBoxFor(m => m.Status.Status1) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.Statuses, new SelectList(Model.Statuses, "Id","Status1"))
    }
    @using (Html.BeginForm("UpdateTicketType", "Specifications"))
    {
        @Html.TextBoxFor(m => m.TicketType.Type) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.TicketTypes, new SelectList(Model.TicketTypes, "Id", "Type"))
    }
    @using (Html.BeginForm("UpdateManufacture", "Specifications"))
    {
        @Html.TextBoxFor(m => m.Manufacture.Name) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.Manufactures, new SelectList(Model.Manufactures, "Id", "Name"))
    }
    @using (Html.BeginForm("UpdateVendor", "Specifications"))
    {
        @Html.TextBoxFor(m => m.Vendor.Name) <input type="submit" value="Add" />

        @Html.ListBoxFor(m => m.Vendors, new SelectList(Model.Vendors, "Id", "Name"))
    }</div>


Viewing all articles
Browse latest Browse all 9386

Trending Articles