Thursday, January 2, 2014

Accessing Model property in MVC View from Javascript

For example, you have following model in your MVC application:

public class Employee
{
public string EmployeeName
public int EmployeeNumber
}

You have bound this model to your MVC view (Razor/ Html), and there may be a case when you need to access "EmployeeName" in the Javascript from that view.

You can access value of "EmployeeName" property of your model by following way:

<script type="text/javascript">
function showEmployeeName()
{
alert('@(Model.EmployeeName)');
}

</script>

23 comments:

  1. FFS! I've just spent the whole night trying to get a string value from my model into my JS. And it all came down to quotes! Cooking with gas now :-) Many thanks.

    ReplyDelete
  2. Great! Thanks a lot!

    ReplyDelete
  3. One help i want to loop one Model property that property in string[] in jquery.can u please tell how to do that?
    Thanks in advance

    ReplyDelete
  4. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  5. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    Selenium training in Chennai

    Selenium training in Bangalore

    Selenium training in Pune

    Selenium Online training

    ReplyDelete
  6. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  7. Attend The Machine Learning Training in Bangalore From ExcelR. Practical Machine Learning Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Machine Learning Training in Bangalore.
    ExcelR Machine Learning Training in Bangalore

    ReplyDelete
  8. I have read your blog its very attractive and impressive. I like it your blog. digital marketing training in bangalore

    ReplyDelete
  9. Awesome,Thank you so much for sharing such an awesome blog.citrix training in bangalore

    ReplyDelete
  10. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info.
    devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

    ReplyDelete
  11. Hey there it awesome to read and explore your blog my friend. Keep creating like this! Thanks for sharing!
    Perl training in bangalore
    Perl institutes in bangalore

    ReplyDelete
  12. This is most informative and also this post most user-friendly and super navigation to all posts.
    Data Science Course in Pune
    Python Classes in Pune

    ReplyDelete
  13. Windows 7 Crack Download 2022 ; Windows 7 Professional Product Key 32-Bit. Windows 7 Professional Product Key 64-Bit ; 24437-XVJQQ-F36R3-7HM2B- .Windows 7 Ultimate Product Key

    ReplyDelete
  14. TorGuard VPN Crack gives you anonymous IP addresses and safe access to the Internet. So, Defend your privacy and data! Your privacy has never been more at risk due to the security holes that hackers, internet restrictions, identity theft, and ISP spying activities have created.
    https://get4pc.org/torguard-vpn-apk-crack/

    ReplyDelete
  15. FL Studio Crack full download is the best music developer and producing software also called Fruity Loops. It is the most popular studio because it contains a Cardinal auditory workplace
    https://crackedpc.org/fl-studio-crack-21-feb-2023-patchkey/

    ReplyDelete

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman