user4523894 user4523894 - 1 year ago 67
ASP.NET (C#) Question

set value submit from collection submit mvc

I want to get in controller and Model and specific value from collection which equal line on which I press button

<table id="Products" class="Products">
<% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{ %>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
<td><%: @Html.ActionLink("Go to second view", "ViewTwo", "Order", Model, null)%></td>
<input type="submit" title="ads" value =<%: Model.NorthOrderDetails[i].ProductID.ToString()%> name=ssad />
<% } %>


Can I set value in submit from collection, for example

<input type="submit" title="ads" value =<%: Model.NorthOrderDetails[i].ProductID.ToString()%> name=ssad />

And this value will equal 17, for example in controller. This work, but how I can change of text in button from value in collection to any text?

I use code of Stephen Muecke, but I edit table because I use aspx page

<td><button type="button" class="delete" data-id="<%:Model.NorthOrderDetails[i].ProductID %>">Delete</button><td>
<td><input type="hidden" name="<%:Model.NorthOrderDetails[i].ProductName %>" value="<%:i %>" /><td>

And, unfortunately the script doesn't call controller

Answer Source

Rather than doing a full post and regenerating the view each time you want to delete an item, you can use ajax to post the items ID value to a controller method that deletes the item in the database and then remove that item from the DOM. This will greatly improve performance and means you can probably avoid using Session.

Change the view to (sorry, but this is Razor syntax)

  @for (int i = 0; i < Model.NorthOrderDetails.Count; i++)
      <td>@Html.LabelFor(Model.NorthOrderDetails[i].ProductID)</td> // ToString not required
      <td>@Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity)></td>
      <td>@Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice)</td>
      <td>@Html.ActionLink("Go to second view", "ViewTwo", "Order", Model, null)</td> // This wont work
        <button type="button" class="delete" data-id="@Model.NorthOrderDetails[i].ProductID">Delete</button><td> // change this
        <input type="hidden" name="@Model.NorthOrderDetails.Index" value="@i" /> // add this
  <input type="submit" value="Save" /> // add this


  1. Your action link will not work (your cannot pass a collection to a GET method) I suspect you mean @Html.ActionLink("Go to second view", "ViewTwo", "Order", new { ID = Model.NorthOrderDetails[i].ProductID }, null) so you can pass the productID to the ViewTwo() method
  2. Change the submit button in each row to a normal button, and add one submit button at the end (to save all changes to your textboxes in one post)
  3. Add the special hidden input for an Index property. This is used by the DefaultModelBinder to match up collections where the indexers are non-consecutive (which they will be if you delete items in the middle of the collection)
  4. You don't render any input for the ProductID which means you wont be able to identify the products on post back. You will need to add a hidden input for it

Then add the following script

var url = '@Url.Action("Delete", "YourControllerName")';
$('.delete').click(function() {
  var id = $(this).data('id'); // Get the product ID
  var row = $(this).closest('tr') // Get the table row
  $.post(url, { ID: id }, function(data) {
    if(data) {
      row.remove(); // remove the row from the table
    } else {
      // oops!

And the controller

public ActionResult View(IEnumerable<YourModel> model)
  // Save your collection and redirect

public JsonResult Delete(int ID)
  // Delete the product in the database based on the ID
  return Json(true);

Note: If deleting an item could throw and exception of fail in some way, then you should return Json(null); so it can be checked in the ajax method.