最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to pass a razor view modal property value to a javascript function - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited Aug 4, 2016 at 10:20 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Nov 22, 2013 at 9:23 CesarMiguelCesarMiguel 3,8304 gold badges25 silver badges34 bronze badges 7
  • 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
 |  Show 2 more ments

1 Answer 1

Reset to default 6

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

});
发布评论

评论列表(0)

  1. 暂无评论