Aswin Alagappan
Aswin Alagappan

Reputation: 173

Place a Razor Code inside a JavaScript var

I wrote a javascript function in which new row gets added in the table. For static data it is working fine, how to place Razor tags inside those javascript var

var count = "1";
var strHtml1 ="@Html.CheckBoxFor(x => x[count].isChecked)";
count++;

This line doesn't seem to work. How should I place the razor code inside the var and also the count [var] inside that razor code.

Upvotes: 0

Views: 108

Answers (2)

Nikitesh
Nikitesh

Reputation: 1305

Just use single quotes

var count = "1";
var strHtml1 ='@Html.CheckBoxFor(x => x[count].isChecked)';
count++;

If it is just the value of checkbox then you can get the value using jquery

var count = $('.count:checked').val(); 

Upvotes: 0

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

Here your count variable is javascript . Can you try to make whole count logic in razor and strHtml out like this ?

@{
var count = "1";
var strHtml1 =Html.CheckBoxFor(x => x[count].isChecked);
count++;
}
//and in js
var jsStrHmtl='@strHtml1';

Upvotes: 3

Related Questions