Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help in JSON / JavaScript

    I'm making a C # ASP.NET MVC3 project.
    I use Visual Studio 2010.
    I have my Create View of OrdemServico. I want to create through JSON a way to populate the DropDownList fetching data in the database.
    I'm having trouble applying the Create JSON and OrdemServicoController (specifically in HttpPost).
    Have found several way to use Json but not understood very well, and could not apply.
    If you can give me a hand.

  • #2
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my JavaScript

    function Control() {
    var x = document.getElementById("IdFornecedor");
    var option = document.createElement("option");
    option.text = "T1";
    option.value = "1";
    x.add(option);

    var y = document.getElementById("IdConsultor");
    var option = document.createElement("option");
    option.text = "T2";
    option.value = "1";
    y.add(option);

    var z = document.getElementById("IdAtividade");
    var option = document.createElement("option");
    option.text = "T3";
    option.value = "1";
    z.add(option);

    var w = document.getElementById("IdClassificacao");
    var option = document.createElement("option");
    option.text = "T4";
    option.value = "1";
    w.add(option);

    }

  • #3
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My Create.cshtml

    @model ControleOS.Models.OrdemServico

    @{
    ViewBag.Title = "Create";
    }

    <h3>Create</h3>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/JScript1.js")" type="text/javascript"></script>

    @using (Html.BeginForm()){
    @Html.ValidationSummary(true)

    <fieldset>
    <legend>Ordem de Serviço</legend>

    @Html.HiddenFor(model => model.Id)

    <div class="editor-label">
    @Html.LabelFor(model => model.IdFornecedor)
    </div>
    <div class="editor-field">
    <select id="IdFornecedor" name="IdFornecedor" onchange="Control()">
    <option value="0">Selecione fornecedor</option>
    <option value="1">Totvs</option>
    </select>
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.IdConsultor)
    </div>
    <div class="editor-field">
    <select id="IdConsultor" name="IdConsultor" onchange="Control()">
    </select>
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.IdAtividade)
    </div>
    <div class="editor-field">
    <select id="IdAtividade" name="IdAtividade" onchange="Control()">
    </select>
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.IdClassificacao)
    </div>
    <div class="editor-field">
    <select id="IdClassificacao" name="IdClassificacao" onchange="Control()">
    </select>
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.Data)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Data)
    @Html.ValidationMessageFor(model => model.Data)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.InicioManha)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.InicioManha)
    @Html.ValidationMessageFor(model => model.InicioManha)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.FimManha)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.FimManha)
    @Html.ValidationMessageFor(model => model.FimManha)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.InicioTarde)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.InicioTarde)
    @Html.ValidationMessageFor(model => model.InicioTarde)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.FimTarde)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.FimTarde)
    @Html.ValidationMessageFor(model => model.FimTarde)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.InicioNoite)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.InicioNoite)
    @Html.ValidationMessageFor(model => model.InicioNoite)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.FimNoite)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.FimNoite)
    @Html.ValidationMessageFor(model => model.FimNoite)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.Descricao)
    </div>
    <div class="editor-field">
    @Html.TextAreaFor(model => model.Descricao, 8, 35, new { @style = "resize: none;" })
    @Html.ValidationMessageFor(model => model.Descricao)
    </div>

    <br />
    <input type="submit" value="Create" />
    </fieldset>
    }

    <div>
    @Html.ActionLink("Back to List", "Index")
    </div>

  • #4
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my OrdemServicoController.cs


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ControleOS.Models;

    namespace ControleOS.Controllers
    {
    public class OrdemServicoController : Controller
    {
    private masterEntities db = new masterEntities();

    //
    // GET: /OrdemServico/

    public ViewResult Index()
    {
    var ordemservico = db.OrdemServico.Include("Atividade").Include("Classificacao").Include("Consultor").Include("Forneced or");
    return View(ordemservico.ToList());
    }


    //
    // GET: /OrdemServico/Details/5

    public ViewResult Details(int id)
    {
    OrdemServico ordemservico = db.OrdemServico.Single(o => o.Id == id);
    return View(ordemservico);
    }

    //
    // GET: /OrdemServico/Create

    public ActionResult Create()
    {
    ViewBag.IdAtividade = new SelectList(db.Atividade, "Id", "Descricao");
    ViewBag.IdClassificacao = new SelectList(db.Classificacao, "Id", "Descricao");
    ViewBag.IdConsultor = new SelectList(db.Consultor, "Id", "NomeCompleto");
    ViewBag.IdFornecedor = new SelectList(db.Fornecedor, "Id", "NomeEmpresa");
    return View();
    }

    //
    // POST: /OrdemServico/Create

    [HttpPost]
    public ActionResult Create(OrdemServico ordemservico)
    {
    if (ModelState.IsValid)
    {
    db.OrdemServico.AddObject(ordemservico);
    db.SaveChanges();
    return RedirectToAction("Index");
    }

    ViewBag.IdAtividade = new SelectList(db.Atividade, "Id", "Descricao", ordemservico.IdAtividade);
    ViewBag.IdClassificacao = new SelectList(db.Classificacao, "Id", "Descricao", ordemservico.IdClassificacao);
    ViewBag.IdConsultor = new SelectList(db.Consultor, "Id", "NomeCompleto", ordemservico.IdConsultor);
    ViewBag.IdFornecedor = new SelectList(db.Fornecedor, "Id", "NomeEmpresa", ordemservico.IdFornecedor);
    return View(ordemservico);
    }

    //
    // GET: /OrdemServico/Edit/5

    public ActionResult Edit(int id)
    {
    OrdemServico ordemservico = db.OrdemServico.Single(o => o.Id == id);
    ViewBag.IdAtividade = new SelectList(db.Atividade, "Id", "Descricao", ordemservico.IdAtividade);
    ViewBag.IdClassificacao = new SelectList(db.Classificacao, "Id", "Descricao", ordemservico.IdClassificacao);
    ViewBag.IdConsultor = new SelectList(db.Consultor, "Id", "NomeCompleto", ordemservico.IdConsultor);
    ViewBag.IdFornecedor = new SelectList(db.Fornecedor, "Id", "NomeEmpresa", ordemservico.IdFornecedor);
    return View(ordemservico);
    }

    //
    // POST: /OrdemServico/Edit/5

    [HttpPost]
    public ActionResult Edit(OrdemServico ordemservico)
    {
    if (ModelState.IsValid)
    {
    db.OrdemServico.Attach(ordemservico);
    db.ObjectStateManager.ChangeObjectState(ordemservico, EntityState.Modified);
    db.SaveChanges();
    return RedirectToAction("Index");
    }
    ViewBag.IdAtividade = new SelectList(db.Atividade, "Id", "Descricao", ordemservico.IdAtividade);
    ViewBag.IdClassificacao = new SelectList(db.Classificacao, "Id", "Descricao", ordemservico.IdClassificacao);
    ViewBag.IdConsultor = new SelectList(db.Consultor, "Id", "NomeCompleto", ordemservico.IdConsultor);
    ViewBag.IdFornecedor = new SelectList(db.Fornecedor, "Id", "NomeEmpresa", ordemservico.IdFornecedor);
    return View(ordemservico);
    }

    //
    // GET: /OrdemServico/Delete/5

    public ActionResult Delete(int id)
    {
    OrdemServico ordemservico = db.OrdemServico.Single(o => o.Id == id);
    return View(ordemservico);
    }

    //
    // POST: /OrdemServico/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
    OrdemServico ordemservico = db.OrdemServico.Single(o => o.Id == id);
    db.OrdemServico.DeleteObject(ordemservico);
    db.SaveChanges();
    return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
    db.Dispose();
    base.Dispose(disposing);
    }
    }
    }

  • #5
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I click on the button "create" on page Create.cshtml, it does not send the page with the data.
    I do not know why it is not sending.
    I've searched the web but have not found exactly what I wanted.
    Told me they would have to create a JSON to fetch the data in SQL Server and populate the DropDownList.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •