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

how to retrieve data from form

$
0
0

I'm working on an asp.net core mvc project and need to know the correct way of collecting the data here and saving it to my database. I'm fairly new to asp.net core but it seems like there are many different ways so I'm going to try to be pretty specific here with how the project is laid out. I have a Business Logic project connecting with my database. I then have a TicketRequestViewModel with all the necessary properties for the form that will be filled out. I then have a controller file with 2 controllers in it. One is to collect the information from the database that will populate the dropdowns for form. The other controller is a HttpPost of the first one, it sets the ticket requestor name, the date time of creation, and I'm assuming it also needs to collect the data from the form? Below I will post the code for the view, model, and controllers. Hopefully I'm doing this correctly and someone can point me in the right direction on how to collect the data from the form and put it into the HttpPost, to collect it all together, then put that ticket objects information into the database.

View:

@using Maintenance.BusinessLogic.Model
@using Maintenance.Web.Controllers
@model TicketRequestViewModel<link rel="RequesterStyleSheet" type="text/css" href="css/RequesterStyleSheet.css"/><div>
        @using (Html.BeginForm("TicketRequest", "Home"))
        {<div>Serial # (Facility # if unavailable): @Html.TextBoxFor(m => m.SerialNum)</div><div>Priority: @Html.DropDownListFor(m => m.Priorities, new SelectList(Model.Priorities,"Id", "PriorityType"))</div><div>TicketType: @Html.DropDownListFor(m => m.TicketTypes, new SelectList(Model.TicketTypes, "Id", "Type"))</div><div>Condition: @Html.DropDownListFor(m => m.Conditions, new SelectList(Model.Conditions, "Id", "Condition1"))</div><div>Desription: @Html.TextAreaFor(m => m.Description, 10, 150, null)</div><input type="submit" value="Submit"/>
        }</div>

Controller:

  public IActionResult TicketRequest(TicketRequestViewModel model)
        {
            var context = new MaintenanceContext();
                // collect options for drop down menu's on ticket request form
            model.Conditions = context.Condition.ToList();
            model.TicketTypes = context.TicketType.ToList();
            model.Priorities = context.Priority.ToList();
            return View(model);
        }

        [HttpPost]
        public IActionResult TicketRequest(Ticket ticket)
        {
            var context = new MaintenanceContext();

            ticket.CreateDate = DateTime.Now; //set date time when form is submitted
            ticket.StatusId = context.Status.FirstOrDefault(s => s.Status1.ToUpper() == "COMPLETE")?.Id; //set status to open when form is submitted
            //TODO: once SSO is completed, add functionality here to automatically set ticket requestor
            return RedirectToAction("");
        }

TicketRequestViewModel:

   public class TicketRequestViewModel
    {
        public List<Condition> Conditions { get; set; }

        public List<TicketType> TicketTypes { get; set; }

        public List<Priority> Priorities { get; set; }

        public string Description { get; set; }

        public string SerialNum { get; set; }


    }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>