Solved

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

Posted on 2012-04-07
5
502 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

740 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