0

this is my list in c# view

@model SmartFeedBackModel.Models.Question;
@{
ViewData["Title"] = "Index";
}

@{
List<SmartFeedBackModel.Models.Question> list = ViewBag.Questions;

 }

this is my java script function

var i = 0;
    var qustid = 0 ;
    function aaa() {
        var a = @list[0].Answer1;
        alert(a);
        qustid = 22;
        i = i + 1;
    }

cannot assign value to variable a

3
  • both list and the js function are on the same view.so i need to get the list elements inside the js function and after that will display one by one manually by button click..if you have a better solution,let me know.thanks in advanced Commented Jan 18, 2018 at 6:00
  • if you able to show an alert this element "@list[0].Answer1" .its better Commented Jan 18, 2018 at 6:04
  • @NightOwl888 Not actually, the list value will be generated and inserted to html (if the script in same view). sandun If you open your browsers console, you should see there error, which will look something like this ReferenceError: <Answer1 value> is not defined. Commented Jan 18, 2018 at 6:06

2 Answers 2

2

You cannot use C# Objects directly in JavaScript. What you are trying to do is something like this.

On Server
--> Get the object & assign it to list.
--> create a script where line is var a = value of @list[0].Answer1

On Client 
--> Try to run  a = value of @list[0].Answer1

This doesn't work. Instead, you need to parse the list to JSON or JS Array. So, it will be something like this.

var jsList = JSON.parse('@Html.Raw(Json.Serialize(list))');
var i = 0;
var qustid = 0;
    function aaa() {
        var a = jsList[i].Answer1;
        alert(a);
        qustid = 22;
        i = i + 1;
    }

I'm assuming you need all the answers on client side and after calling aaa() you need to display next answer.

This is for converting C# array to JS Array.

How do I convert a C# List<string[]> to a Javascript array?

Edit: Json.Encode is not supported in .NET Core. Instead use Json.Serialize()updated the above code.

Sign up to request clarification or add additional context in comments.

7 Comments

You can use c# objects in JS, if he add quotes '' or "" it will work fine, but anyway, it is not the best way to achieve that.
@SeM I know, I think what he want is that entire object. Not just one value.
I recommend this answer, its best to load the data to the client side then do whatever needs to be done on the JavaScript side. To be quiet frank, try reducing razor code to avoid slowness in rendering the page (since it is server side) - which is my biggest issue that I am facing with razor.
The best solution would be, if he will create a proper View Model, then use its values.
there is an error in Json.Encode .what should i do to avoid it
|
0

Try changing @list[0].Answer1 to '@list[0].Answer1'

This will allow you to assign string razor values to a javascript variable.

1 Comment

var i =0 ; function aaa() { a = '@list[i].QuestionID'; alert(a); i = i + 1; } //here i need increment the i value..but error occurs saying name 'i' does not exist in the current context.plz hlp if you knok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.