How to make the custom select of the screen "Suppliers" search information (Company name) in the table "Company"

Asked

Viewed 49 times

0

I am developing an Asp.Net application using MVC 5. I have created one select on the Vendors screen and I would like to know how I can get the information from my bank and show them in this custom.

My application has two screens: Companies and Suppliers. In the suppliers screen, I have a select where I would like to get the names of companies registered on the screen of "Companies".

In the database I have two tables: Suppliers and Companies.

Code of select:

       <div class="form-row">
        <div class="col-7">
            <label for="txtNome">Empresa:</label>
            <select name="Empresa" id="txtEmpresa"  class="custom-select">
                <option selected>Escolha uma empresa...</option>
                <option value="@Model.Empresa">One</option>
                <option value="@Model.Empresa">Two</option>
                <option value="@Model.Empresa">Three</option>
            </select>
        </div>
        <div class="col">
            <label>&nbsp</label>
            <div class="mb-3">
                <a href="~/Empresas/AddEdit" class="btn btn-outline-info"><i class="fas fa-plus"></i>Nova Empresa</a>
            </div>
        </div>
    </div>

inserir a descrição da imagem aqui

  • Why don’t you use Razor? Another detail, add your font controller.

  • Was able to verify the answer?

2 answers

0

Considering Visual Studio’s own MVC template and adding Controllers and Views using the option Add > New Scaffolded Item..., the result you expect can be obtained with the following creation and editing methods:

        // GET: Suppliers/Create
        public IActionResult Create()
        {
            ViewData["CompanyId"] = new SelectList(_context.Company, "Id", "Name");
            return View();
        }

        // POST: Suppliers/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("Id,Name,CompanyId")] Supplier supplier)
        {
            if (ModelState.IsValid)
            {
                _context.Add(supplier);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["CompanyId"] = new SelectList(_context.Company, "Id", "Name", supplier.CompanyId);
            return View(supplier);
        }

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

            var supplier = await _context.Supplier.FindAsync(id);
            if (supplier == null)
            {
                return NotFound();
            }
            ViewData["CompanyId"] = new SelectList(_context.Company, "Id", "Name", supplier.CompanyId);
            return View(supplier);
        }

        // POST: Suppliers/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(int id, [Bind("Id,Name,CompanyId")] Supplier supplier)
        {
            if (id != supplier.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(supplier);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SupplierExists(supplier.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["CompanyId"] = new SelectList(_context.Company, "Id", "Name", supplier.CompanyId);
            return View(supplier);
        }

The important part of all this code is new SelectList(_context.Company, "Id", "Name"). The 1st parameter of SelectList are the listed objects, the 2nd is the id used as key and Name is the property you want to display on select. To view is exactly the one created by Visual Studio.

0

Consider the following scenario:

Na Controller:

List<Empresa> lstEmpresa = new List<Empresa>();
lstEmpresa.Add(new Empresa() { ID = 1, Nome = "Empresa 01"});
lstEmpresa.Add(new Empresa() { ID = 2, Nome = "Empresa 02"});
lstEmpresa.Add(new Empresa() { ID = 3, Nome = "Empresa 03"});

ViewBag.EmpresaList = new SelectList(lstEmpresa, "ID", "Nome");

Na View:

@Html.DropDownListFor(model => model.Empresa, new SelectList(ViewBag.EmpresaList, "Value", "Text"), new { @class = "form-control" })

There are several models to fill, in my applications I develop using this basic concept. Fill the List, you choose how your application does this, but the structure presented is the basic model for understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.