Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Problem in writing a razor code in Script

This is my code. I want to create two textboxes using Razor in script.

the model=>model[i].Name1 is not woring

    var counter = 0;
    @{
        int i = 0;
    }
    $(document).ready(function () {
        $("#addButton").click(function () {
            if (counter < 0) {
                alert("Add more textbox");
                return false;
            }
            i = counter;
            var newTextBoxDiv = $(document.createElement('div'))
                .attr("id", 'TextBoxDiv' + counter).attr("class", 'TextBoxDiv').attr("style", "border:5px solid green");
            newTextBoxDiv.after().html('<label>Name #'+ counter + ': </label>' +
                '@Html.EditorFor(model => model[i].Name1);' +
                '<br/> <label>Email #'+ counter +': </label>' +
               '@Html.EditorFor(model => model[i].EmailID);' +
                '<br/><input type="button" name="button' + counter +
                '" class="removeButton" id="removeButton' + counter + '" value="Remove Button" onclick="remove(this)">');
                   newTextBoxDiv.appendTo("#TextBoxesGroup");
                   alert(i.toString());
                   counter++;
                   i = i + 1;
        });
    });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You cannot get JS to write out Razor - the JS happens client side AFTER the razor has been executed on the server and sent to the client. the JS won't know what to do with the razor syntax.

In this case, you would need to manually create the html which the Razor helper would make for you. It should still parse back into razor, providing you put the html in the right format (right name, id etc)

So write out the and make the name field match what the razor would have generated.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try to set a js variable with the value of model,and then use <input/> to replace @Html.EditorFor():

var counter = 0;
var m = JSON.parse('@Json.Serialize(@Model)');
    $(document).ready(function () {
        $("#addButton").click(function () {
            if (counter < 0) {
                alert("Add more textbox");
                return false;
            }
            var newTextBoxDiv = $(document.createElement('div'))
                .attr("id", 'TextBoxDiv' + counter).attr("class", 'TextBoxDiv').attr("style", "border:5px solid green");
            newTextBoxDiv.after().html('<label>Name #'+ counter + ': </label>' +
                '<input id="model_' + counter +'__Name1" name=" model[' + counter + '].Name1" value="' + m[counter].Name1 + '"/>' +
                '<br/> <label>Email #'+ counter +': </label>' +
               '<input id="model_' + counter +'EmailID" name=" model[' + counter + '].EmailID" value="' + m[counter].EmailID + '"/>' +
                '<br/><input type="button" name="button' + counter +
                '" class="removeButton" id="removeButton' + counter + '" value="Remove Button" onclick="remove(this)">');
                   newTextBoxDiv.appendTo("#TextBoxesGroup");
                   counter++;
        });
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda