C# MVC solution does not run Javascript on IIS

Asked

Viewed 470 times

0

I have a C# solution where I use MVC. I have a View for product items. In this view I have a Droplist as specified below:

Drop List

    <div class="form-group">
    @Html.LabelFor(model => model.ProdutoId, "Produto", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("ProdutoId", null, String.Empty, htmlAttributes: new { @class = "form-control", onchange = "PesquisaProduto(value)" })
        @Html.ValidationMessageFor(model => model.ProdutoId, "", new { @class = "text-danger" })
    </div>
</div>

I use Javascript to access an Action in the Controller so that, when selecting a product, a text with the sale value of the selected product is filled in. Below the script:

Script:

<script type="text/javascript">
    function PesquisaProduto(codigoProdutoId) {
        var url = "/ItemPedidoes/DadosProdutos/" + codigoProdutoId;
        $.get(url, null, function (data) {
            $("#ValorUnitario").val(data);
        })
    }

Controller:

public decimal DadosProdutos(int? id)
{
    decimal _return = 0;
    if (id == null) {
        _return = 0;
    } else
    {
        var produtoSelecao = db.Produtos.Find(id);

        if (produtoSelecao.PrecoVenda == null || produtoSelecao.PrecoVenda == 0)
        {
            _return = 0;
        }
        else
        {
            _return = (decimal)produtoSelecao.PrecoVenda;
        }

    }
    return _return;
}

It happens that, in the tests, running in Visual Studio, everything works perfectly, but when installing in IIS the script does not work. It does not fill in the value, as if the script is in error..

Would anyone know if there is any configuration in the IIS that should be changed?

My version of IIS is 10.0.14393.0 and I run on a windows 10 machine.

  • 1

    Young man, make sure the console shows no error. Also check the network tab of your browser.

  • Dear.. I was able to fix it.. I actually needed to change the way I was mounting the URL.. I saw in the Log the IIS that GET was returning 404!!..

  • I’ve had several problems calling the controller in the ajax call like this ../controller/exemplo, when sent to the server does not work, after much hitting the head I found that always works like this in your case instead of var url = "/ItemPedidoes/DadosProdutos/", use var url = "@Url.Action("DadosProdutos", "ItemPedidoes")"

  • This is Vinicius.. I’m using ".. /.. /Controller/Action/code".. It was certified in both IIS and Visualstudio.. Vlw!!

2 answers

2

Based on the friend’s tip @Linq , I checked the IIS log and found the following line there:

2017-10-11 17:13:16 ::1 GET /Loja4/ItemPedidoes/Create/~/ItemPedidoes/DadosProdutos/1 - 80 - ::1 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+rv:56.0)+Gecko/20100101+Firefox/56.0 http://localhost/Loja4/ItemPedidoes/Create/3 404 0 2 5

That is, the URL returned 404 on the server and for the URL to be passed correctly in GET I had to change the "mount" of the same..

Below as was the script:

<script type="text/javascript">
    function PesquisaProduto(codigoProdutoId) {
        var url = "/../../ItemPedidoes/DadosProdutos/" + codigoProdutoId;
        $.get(url, null, function (data) {
            $("#ValorUnitario").val(data);
        })
    }
  • It would be better to use a function to return the correct path. With this modification it will no longer work the old way...

  • What do you call working the old-fashioned way @LINQ? It’s actually working on both IIS and Visualstudio.

0

Change the script call to:

<script type="text/javascript">
    function PesquisaProduto(codigoProdutoId) {
        var url = "@Url.Action("DadosProdutos")/" + codigoProdutoId;
        $.get(url, null, function (data) {
            $("#ValorUnitario").val(data);
        })
    }

Browser other questions tagged

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