0

I am retrieving a list of values using List and I am using for loop. The requirement is here I have two buttons, one to submit and the other to delete a record.When I click btnDelete button I want to delete that particular record.

I am having the object Product as
public partial class Product
{
    public int Id { get; set; }
    public string Model_Name { get; set; }        
    public Nullable<int> Price { get; set; }
    public Nullable<int> Total_Products { get; set; }
}

Now I want the property Id of the Product to come and sit in the button's Id so that I could retrieve the complete record and delete it using Jquery. I have done with jquery here I just want to assign the button's Id with the value from Model. I am having the following code

@model List<Shop_Online.com.Models.Product>
<script type="text/javascript">
//some script here
</script>
@using (Html.BeginForm())
{
<div style="width: 860px; margin: 0 auto" class="main">
    <table border="1" style="font-family: Verdana; font-size: 13px" class="table">
        <tr style="background-color: #f2f2f2; height: 30px">
            <th colspan="4">ITEM</th>
            <th>DELIEVERY DETAILS</th>
            <th>QTY</th>
            <th>SUB TOTAL</th>
            <th>Remove Items</th>
        </tr>
        <tbody>
@for(int i=0;i<Model.Count;i++)
{
<tr>      
    @Html.HiddenFor(x=>x[i].Id)
    <td colspan="4" > @Html.DisplayFor(x=>x[i].Model_Name)</td>
    <td style="width: 39%">Free Delievery Delivered in 2-3 business days.</td>
    <td>@Html.TextBoxFor(x=>x[i].Total_Products)</td>
    <td>@Html.DisplayFor(x=>x[i].Price)</td>
    <td>
    //something like this
    //btnDelete
        <button type="submit" class="btnDelete"  value="x[i].Id" id="x[i].Id" 
        name="Action">Remove</button></td>
    </tr>
    }
    </tbody>
    </table>
    <input type="submit" value="Submit" id="btnPost"/>
    </div>
    }
  If I use foreach loop then I have

 @foreach(var item in model) then I can use the following within block as
 <button type="submit" class="remove" value="@item.Id" id="@item.Id" 
 name="Action">Remove</button>

Thanks

Zaker
  • 537
  • 16
  • 30

1 Answers1

0

As @Mark Shechenko already wrote:

<button type="submit" class="btnDelete" value="@Model[i].Id" id="@Model[i].Id" name="Action">Remove</button>

BUT... If the Id is a number, you have to remember that there are rules for ID and NAME attributes in html, see the answer to this question: What are valid values for the id attribute in HTML?

Community
  • 1
  • 1
Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36