How to add a javascript variable inside a razor list











up vote
-1
down vote

favorite












Hi all I'm really confused in adding a var parameter inside a razor object and the answers I found didn't resolve my issue. How do I correctly do this. I'm new to Razor binding



Here's my script



<script>
$(document).ready(function () {


function addRow(){
var id = 1;
var html= '<tr>' +
'<td><input id="TxtExtent1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN"></td>' +
'<td><input id="TxtRanks1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_RANK_EN"></td>' +
'<td><input id="TxtCategory1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_CATEGORY_EN"></td>' +
'<td><input id="TxtStandardgrades1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_STD_GRADES"></td>' +
'<td><input id="TxtTotalweight1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_TOTAL_WEIGHTED_SCORE"></td>' +
'<td><input id="TxtIQscores1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_IQ_SCORES_EN"></td>' +
'<td><input type="button" class="BtnPlus" value="+" /></td>' +
'<td><input type="button" class="BtnMinus" value="-" /></td>' +
+'<tr>'

$(html).appendTo($("#TableExtent"))
id++;
};

$("#TableExtent").on("click", ".BtnPlus", addRow);

});




</script>


As you can see How do I add this var id inside the FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN. help would be appreciated.










share|improve this question






















  • Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
    – Camilo Terevinto
    Nov 8 at 11:46












  • @CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
    – Ashane Alvis
    Nov 8 at 11:47








  • 1




    You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
    – Camilo Terevinto
    Nov 8 at 11:48












  • Can you give me an example? On how to convert this?
    – Ashane Alvis
    Nov 8 at 11:50










  • I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
    – Camilo Terevinto
    Nov 8 at 11:51















up vote
-1
down vote

favorite












Hi all I'm really confused in adding a var parameter inside a razor object and the answers I found didn't resolve my issue. How do I correctly do this. I'm new to Razor binding



Here's my script



<script>
$(document).ready(function () {


function addRow(){
var id = 1;
var html= '<tr>' +
'<td><input id="TxtExtent1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN"></td>' +
'<td><input id="TxtRanks1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_RANK_EN"></td>' +
'<td><input id="TxtCategory1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_CATEGORY_EN"></td>' +
'<td><input id="TxtStandardgrades1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_STD_GRADES"></td>' +
'<td><input id="TxtTotalweight1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_TOTAL_WEIGHTED_SCORE"></td>' +
'<td><input id="TxtIQscores1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_IQ_SCORES_EN"></td>' +
'<td><input type="button" class="BtnPlus" value="+" /></td>' +
'<td><input type="button" class="BtnMinus" value="-" /></td>' +
+'<tr>'

$(html).appendTo($("#TableExtent"))
id++;
};

$("#TableExtent").on("click", ".BtnPlus", addRow);

});




</script>


As you can see How do I add this var id inside the FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN. help would be appreciated.










share|improve this question






















  • Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
    – Camilo Terevinto
    Nov 8 at 11:46












  • @CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
    – Ashane Alvis
    Nov 8 at 11:47








  • 1




    You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
    – Camilo Terevinto
    Nov 8 at 11:48












  • Can you give me an example? On how to convert this?
    – Ashane Alvis
    Nov 8 at 11:50










  • I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
    – Camilo Terevinto
    Nov 8 at 11:51













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Hi all I'm really confused in adding a var parameter inside a razor object and the answers I found didn't resolve my issue. How do I correctly do this. I'm new to Razor binding



Here's my script



<script>
$(document).ready(function () {


function addRow(){
var id = 1;
var html= '<tr>' +
'<td><input id="TxtExtent1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN"></td>' +
'<td><input id="TxtRanks1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_RANK_EN"></td>' +
'<td><input id="TxtCategory1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_CATEGORY_EN"></td>' +
'<td><input id="TxtStandardgrades1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_STD_GRADES"></td>' +
'<td><input id="TxtTotalweight1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_TOTAL_WEIGHTED_SCORE"></td>' +
'<td><input id="TxtIQscores1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_IQ_SCORES_EN"></td>' +
'<td><input type="button" class="BtnPlus" value="+" /></td>' +
'<td><input type="button" class="BtnMinus" value="-" /></td>' +
+'<tr>'

$(html).appendTo($("#TableExtent"))
id++;
};

$("#TableExtent").on("click", ".BtnPlus", addRow);

});




</script>


As you can see How do I add this var id inside the FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN. help would be appreciated.










share|improve this question













Hi all I'm really confused in adding a var parameter inside a razor object and the answers I found didn't resolve my issue. How do I correctly do this. I'm new to Razor binding



Here's my script



<script>
$(document).ready(function () {


function addRow(){
var id = 1;
var html= '<tr>' +
'<td><input id="TxtExtent1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN"></td>' +
'<td><input id="TxtRanks1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_RANK_EN"></td>' +
'<td><input id="TxtCategory1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_CATEGORY_EN"></td>' +
'<td><input id="TxtStandardgrades1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_STD_GRADES"></td>' +
'<td><input id="TxtTotalweight1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_TOTAL_WEIGHTED_SCORE"></td>' +
'<td><input id="TxtIQscores1" type="text" class="form-control" placeholder="@ResGlobal.ENTER" value="@Model.TabFormViewModel.FORM_7_LIST[0].F7_IQ_SCORES_EN"></td>' +
'<td><input type="button" class="BtnPlus" value="+" /></td>' +
'<td><input type="button" class="BtnMinus" value="-" /></td>' +
+'<tr>'

$(html).appendTo($("#TableExtent"))
id++;
};

$("#TableExtent").on("click", ".BtnPlus", addRow);

});




</script>


As you can see How do I add this var id inside the FORM_7_LIST[0].F7_EXTENT_CONFIDENCE_EN. help would be appreciated.







javascript c# html razor data-binding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 11:40









Ashane Alvis

40111026




40111026












  • Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
    – Camilo Terevinto
    Nov 8 at 11:46












  • @CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
    – Ashane Alvis
    Nov 8 at 11:47








  • 1




    You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
    – Camilo Terevinto
    Nov 8 at 11:48












  • Can you give me an example? On how to convert this?
    – Ashane Alvis
    Nov 8 at 11:50










  • I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
    – Camilo Terevinto
    Nov 8 at 11:51


















  • Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
    – Camilo Terevinto
    Nov 8 at 11:46












  • @CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
    – Ashane Alvis
    Nov 8 at 11:47








  • 1




    You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
    – Camilo Terevinto
    Nov 8 at 11:48












  • Can you give me an example? On how to convert this?
    – Ashane Alvis
    Nov 8 at 11:50










  • I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
    – Camilo Terevinto
    Nov 8 at 11:51
















Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
– Camilo Terevinto
Nov 8 at 11:46






Razor runs on the server, Javascript runs on the client. So the simple answer is: you cannot, and you shouldn't have to. Even more, you shouldn't be concatenating strings to create HTML.
– Camilo Terevinto
Nov 8 at 11:46














@CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
– Ashane Alvis
Nov 8 at 11:47






@CamiloTerevinto Then How do I dynamically resolve this issue? Here I have tried to add more textfield by addRow function.
– Ashane Alvis
Nov 8 at 11:47






1




1




You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
– Camilo Terevinto
Nov 8 at 11:48






You don't, and you don't have to. Create the entire structure using Razor and give the client the last index used: var lastIndex = @i;. Even more, that won't work as you would expect once in the client, the constants will work, but the indexers not.
– Camilo Terevinto
Nov 8 at 11:48














Can you give me an example? On how to convert this?
– Ashane Alvis
Nov 8 at 11:50




Can you give me an example? On how to convert this?
– Ashane Alvis
Nov 8 at 11:50












I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
– Camilo Terevinto
Nov 8 at 11:51




I would do this: 1. Remove that HTML concatenation. 2. create a Partial View with that structure. 3. Create an Action method so you can call it from the client. 4. use AJAX to get that HTML from the server and then append it to #TableExtent as you are already doing.
– Camilo Terevinto
Nov 8 at 11:51

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207013%2fhow-to-add-a-javascript-variable-inside-a-razor-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207013%2fhow-to-add-a-javascript-variable-inside-a-razor-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini