Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I pass parameters to onclick="window.location" in LI

I'm working on MVC4 want to set parameter value from Model inside li onclick window.location

Name = MyValue is hard-coded here

How do I set parameter from Model i'm using foreach loop to iterate.

<ul class="my_list">
        <li style="position: relative" onclick="window.location='/Mypage?Name=MyValue'">
         </li>
</ul>

onclick is in li how can I set my parameter value there ?

like image 801
Neo Avatar asked Feb 13 '23 21:02

Neo


1 Answers

you can do like this:

<ul class="my_list">
  <li style="position: relative" onclick="myFunction(5);">
  </li>
</ul>

With Razor like this:

@foreach(var item in Model)
{
    <li style="position: relative" onclick="myFunction(@(item.SomeProperty));">
    </li>
}

Javascript:

<script type="text/javascript">

function myFunction(id)
{
    window.location='/Mypage?Name='+id;
}

</script>

I am passing 5 but you can pass some variable in it.

like image 185
Ehsan Sajjad Avatar answered Feb 15 '23 09:02

Ehsan Sajjad