1

In my controller I have

public ActionResult Index()
{
    //...
    //...
    var viewModel = new IndexViewModel()
    {
        MTohum = mTohum,
        STohum = sTohum,
        BTohum = bTohum,
        TKota = tKota,
        KKota = kKota,
        BKota = bKota,
    };
    //return Content("tkota : " + tKota.ToString() + " kkota : " + kKota.ToString() + " bkota : " + bKota.ToString());
    return View(viewModel);
}

When I remove comment and return Content() I can see that values are not null.

On the view side:

//..html codes..//

<script>
    $(document).ready(function () {
        alert(@Model.TKota);
        alert(@Model.KKota);
        alert(@Model.BKota);
        alert(@Model.TKota + " " + @Model.KKota + " " + @Model.BKota);
    });
</script>

My problem is: 1st, 2nd and 3rd alerts showing right info but when I trying to take all info in one alert like 4th, I can get @Model.TKota, @Model.KKota but I can't get @Model.BKota value.

Consequently I can't use @Model.BKota in the following lines.

2
  • alert("@Model.TKota + " " + @Model.KKota + " " + @Model.BKota); looks like a syntax error,would occur due to the opening " not being closed. Try this: alert(@Model.TKota + " " + @Model.KKota + " " + @Model.BKota); Commented Nov 19, 2019 at 9:17
  • Sorry I edited my code.Its not about syntax error.Alert1 shows "100", alert2 shows "200", alert3 shows "300" but alert4 shows "100 200 " instead of "100 200 300". Commented Nov 19, 2019 at 10:33

2 Answers 2

3

Your last alert is badly formatted, it should look like this

alert('@Model.TKota @Model.KKota @Model.BKota');

See the following fiddle

https://dotnetfiddle.net/yc8F4Q

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

3 Comments

Sorry I edited my code.Its not about syntax error.Alert1 shows "100", alert2 shows "200", alert3 shows "300" but alert4 shows "100 200 " instead of "100 200 300".
@BerkanToptaş in that case you don't have any value in BKota of the viewmodel, what's in the bKota variable? You could see in my fiddle that this is working.
I solved problem by casting int from double.bKota variable is type of double so after I cast it to int it worked.I think there were some issues about floating numbers..
0

The clue is that you are not able to work with the variables after that statement, this usually means the parser has choked on something due to a syntax error.

alert("@Model.TKota + " " + @Model.KKota + " " + @Model.BKota); looks like a syntax error would occur here due to the opening " not being closed.

Try this:

alert('@Model.TKota @Model.KKota @Model.BKota');

2 Comments

That would not work if TKoda etc are anything else than numeric values
Sorry I edited my code.Its not about syntax error.Alert1 shows "100", alert2 shows "200", alert3 shows "300" but alert4 shows "100 200 " instead of "100 200 300".

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.