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 11 of 11
  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
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You should ask in ASP.NET forum not in JavaScript forum. Ask a moderator to move this thread there.

  • #3
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why should I ask in the forum of asp.net?
    I believe I'm asking in the right forum.
    ok?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Isn't what you're asking is about how to make the C# controller output JSON response? Can you post your code?

  • #5
    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);

    }

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

    //
    // 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);
    }

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

    <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>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    So where exactly in your code you're having trouble with?

  • #9
    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.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I don't see any button in Create.cshtml, I only see dropdowns. And where is the event handler that is invoked when that button is clicked?

  • #11
    New Coder
    Join Date
    Mar 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the complete code 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>


  •  

    Posting Permissions

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