Solved

MVC3 post back a specific Id from a  list of similar items by clicking the list item

Posted on 2012-04-07
5
497 Views
Last Modified: 2012-04-08
I have two lists on a Page.  The items of each list are identified by an Id.  I want to select a list item pass the Id back to the server.  What is the best way?
0
Comment
Question by:ddefreitas
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37819700
It would help if you showed some code or posted a link.


Cd&
0
 

Author Comment

by:ddefreitas
ID: 37819885
The bones of the cshtml page code file goes like this and basically I want to be able to allocate the Id of a list 1 item to one or more items in list 2.  



<ul>
   

@foreach (var item in Model.HeadingsList1) {
    <li>
            <input type="checkbox" name="add" />Id: @item.Id  @item.Name
           
        </li>
}

</ul>

<ul>
   

@foreach (var item in Model.List2) {
    <li>
            <input type="checkbox" name="add" />Id: @item.Id  @item.Name
           
        </li>
}

</ul>

producing

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Lists</title>
</head>

<body>
      <ul>
      <li>
        <input type="checkbox" name="add" />Id: 1  Item 1 from list 2
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 2  Item 2 from list 2
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 3  Item 3 from list 2
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 4  Item 4 from list 2
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 5  Item 5 from list 2
      </li>
    </ul>

    <ul>
      <li>
        <input type="checkbox" name="add" />Id: 1  Item 1 from list 1
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 2  Item 2 from list 1
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 3  Item 3 from list 1
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 4  Item 4 from list 1
      </li>
      <li>
        <input type="checkbox" name="add" />Id: 5  Item 5 from list 1
      </li>
    </ul>

      <input name="Add" type="submit" value="add">

</body>
</html>
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 37819941
Put the inputs an submit into a form.  Give the form a "post" method, and set the action to point at the server side script that handles the update or whatever action you are doing.  If you don't want a page refresh then set the action of the form to a javascript function that goes across to the server using AJAX.

Cd&
0
 

Author Comment

by:ddefreitas
ID: 37821009
I have enclosed the inputs and submits in a Html.BeginForm() and can get the Id back in the controller but the Checkbox doesn't show.  How would I identify which items have been selected?
0
 

Author Comment

by:ddefreitas
ID: 37821694
I have now solved my checkbox id issue as well so thanks for the steer onto the right path
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now