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.
Young man, make sure the console shows no error. Also check the network tab of your browser.
– Jéf Bueno
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!!..
– Leonardo Cruz
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 ofvar url = "/ItemPedidoes/DadosProdutos/"
, usevar url = "@Url.Action("DadosProdutos", "ItemPedidoes")"
– Vinicius
This is Vinicius.. I’m using ".. /.. /Controller/Action/code".. It was certified in both IIS and Visualstudio.. Vlw!!
– Leonardo Cruz