Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!