-2

I have a model and it has one property as Dictionary<string, bool>. There are multiple links and I am calling same javascript method on click and passing link id. Model dictionary key will be link id. so I want to set dictionary value true of false based on some condition using this key. I tried a lot but not able to do, Please help me out. I want to do something like this.

function LinkClick(id){
    string strKey = id; //strKey is C# string
    @Model.DictionaryIdSet[strKey] = true;
}
Nico
  • 3,471
  • 2
  • 29
  • 40
Vivek Jain
  • 17
  • 2
  • Why are you mixing razor and JavaScript? You really need to pick one. If you go JS, you'll use an AJAX remoting call to your MVC Api Controller. – Teagan42 Aug 06 '15 at 19:32
  • Razor runs at server, the only way to archive this is by posting this value to the server. – Fals Aug 06 '15 at 19:37
  • "I want to set dictionary value true of false based on some condition using this key" The dictionary is part of the server side code. The only way to affect it is to post the value to the server (using AJAX for example). You are getting confused between how server-side and client-side work. – nurdyguy Aug 06 '15 at 19:39
  • Keep in mind that your Razor views are executed on the server side, after that they generate HTML and sent to client browser. And is the client browser that execute the JS. So, bassicly it is not bossible to achieve want you want. The only way is to so as @Teagan42. If you can provider more context about your final goal with that dictionary, it will be more easy to give you some advise. – dnlgmzddr Aug 06 '15 at 19:40

2 Answers2

2

You cannot work with C# object in js code (on client side), but what you can do is

  1. GET your dictionary as json - see this post

  2. then POST it like this

Community
  • 1
  • 1
AntonS
  • 341
  • 1
  • 8
0

Try this

  @{
           Model.DictionaryIdSet[strKey] = true;
  }
Dakota
  • 495
  • 2
  • 10
  • 2
    Please explain what your code does and why it will solve the problem. An answer that just contains code (even if it's working) usually wont help the OP to understand their problem. I also recommend you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue. – SuperBiasedMan Aug 07 '15 at 08:25
  • It's not a guess , i used above code in my project and it worked well. – Dakota Aug 13 '15 at 09:25
  • Then please edit the answer to explain that. An answer that just says "Try this" with code doesn't really explain much. – SuperBiasedMan Aug 13 '15 at 09:26