i try to send razor value to jquery funcion.
View:
@Html.TextBoxFor(model => model.NumTransportado, new { @class = "form-control input-sm", id = "NumTransResp" + Model.ServicosID + "", name = "NumTransResp" + Model.ServicosID + "", onchange = "PreencheDadosVendedor(this, 3, " + @Model.ServicosID + ")" })
i can use all parameters except " + @Model.ServicosID + "
Js file:
function PreencheDadosVendedor(idVend, tipoFuncionario, idServicoEdicao) {
$.getJSON("/Contrato/getDadosVendedor", { id: $(idVend).val(), tipoFuncionario: tipoFuncionario },
function (result) {
switch (tipoFuncionario) {
case 1:
$("#NomeVendedor_Contrato").val(result.NomeVendedor);
case 2:
$("#NomeTransResponsavel").val(result.NomeVendedor);
case 3:
$("#NomeTransResponsavel_" + idServicoEdicao + "").val(result.NomeVendedor);
}
});
}
i try to do idSericoEdicao
is my @Model.ServicosID
, but i have same result: undifined
i try to send razor value to jquery funcion.
View:
@Html.TextBoxFor(model => model.NumTransportado, new { @class = "form-control input-sm", id = "NumTransResp" + Model.ServicosID + "", name = "NumTransResp" + Model.ServicosID + "", onchange = "PreencheDadosVendedor(this, 3, " + @Model.ServicosID + ")" })
i can use all parameters except " + @Model.ServicosID + "
Js file:
function PreencheDadosVendedor(idVend, tipoFuncionario, idServicoEdicao) {
$.getJSON("/Contrato/getDadosVendedor", { id: $(idVend).val(), tipoFuncionario: tipoFuncionario },
function (result) {
switch (tipoFuncionario) {
case 1:
$("#NomeVendedor_Contrato").val(result.NomeVendedor);
case 2:
$("#NomeTransResponsavel").val(result.NomeVendedor);
case 3:
$("#NomeTransResponsavel_" + idServicoEdicao + "").val(result.NomeVendedor);
}
});
}
i try to do idSericoEdicao
is my @Model.ServicosID
, but i have same result: undifined
-
And does
@Model.ServicosID
actually have a value in your view model? – musefan Commented Nov 22, 2013 at 9:26 - Yes, have value. I use it several times – CesarMiguel Commented Nov 22, 2013 at 9:29
- View the source of the HTML page (in browser), find that onchange attribute. What does it look like? – musefan Commented Nov 22, 2013 at 9:30
- it looks fine, do what i want – CesarMiguel Commented Nov 22, 2013 at 9:32
-
The html in browser:
<input class="form-control input-sm" data-val="true" data-val-number="The field NumTransportado must be a number." id="NumTransResp683" name="NumTransportado" onchange="PreencheDadosVendedor(this, 3, 683)" type="text" value="4">
The 683 it value i want – CesarMiguel Commented Nov 22, 2013 at 9:34
1 Answer
Reset to default 6The reason you are getting undefined (Assuming value is a string) is because you need to wrap @Model.ServicosID
it in quotes, otherwise it will look for the value of the property as the name of the variable, instead you just need to pass the value as the a string.
Try this:-
@Html.TextBoxFor(model => model.NumTransportado,
new { @class = "form-control input-sm",
id = "NumTransResp" + Model.ServicosID + "",
name = "NumTransResp" + Model.ServicosID + "",
onchange = "PreencheDadosVendedor(this, 3, '" + @Model.ServicosID + "')" });
^____ ^____
Or better do this, instead of adding an onchange
attribute; attach an event instead and make use of data-*
attributes to store element specific values, this way you separate out html and js:
i.e:
@Html.TextBoxFor(model => model.NumTransportado,
new { @class = "form-control input-sm myclass", //Add a class
id = "NumTransResp" + Model.ServicosID + "",
data_tipoFuncionario = 3, //Add a data attribute
name = "NumTransResp" + Model.ServicosID + ""});
and
$('.myclass').change(function(e){
var idServicoEdicao = this.id.replace("NumTransResp",""), //get the part from its own id
tipoFuncionario = $(this).data("tipoFuncionario"); //get the value from data attribute
$.getJSON("/Contrato/getDadosVendedor", { id: this.value, tipoFuncionario: tipoFuncionario },
function (result) {
switch (tipoFuncionario) {
case 1:
$("#NomeVendedor_Contrato").val(result.NomeVendedor);
case 2:
$("#NomeTransResponsavel").val(result.NomeVendedor);
case 3:
$("#NomeTransResponsavel_" + idServicoEdicao).val(result.NomeVendedor);
}
});
});