0

I have list of model and send it to view. i want delete a few row of model list. how to delete from list in view?

List<Booking> models = new List<Booking>();
foreach (string id in items)
{
   models.Add(
             new Booking()
                 { ...}
}
return View(models);

and into view:

  <main id="BigPanel" class="flexcol border_radius" role="main">
     @using (Html.BeginForm("FinalReserve", "Booking", FormMethod.Post, new { @class = "cart-rooms flexcol" }))
    {
        for (int i = 0; i < Model.Count; i++)
        {
            <div class="cart-room setborder border_radius flexrow">

                @Html.HiddenFor(m => Model[i].Price)
                @Html.HiddenFor(m => Model[i].RRId)
                @Html.HiddenFor(m => Model[i].CheckOutDate)
                <span>@Model[i].FromDate</span>
                <span>@Model[i].CheckInDayName</span>
                <span>@Model[i].DiscountPercent</span>
                <span>@Model[i].Price</span>
                <button onclick="removelist(@Model[i].RRId)"> Delete</button>
            </div>

        }
        <hr style="width:100%;" />
        <div class="cart-room flexrow">
            <div class="sum flex1 flexcol">
               <div class="flexrow">
                    <span class="flex1">@Model.Sum(m => m.Price)</span>
               </div>
               <div class="paybtn-group">
                  <button class="btns" onclick="history.go(-1); return false;">    back       </button>
                    <button class="mbtn_green btn_cart" type="submit">
                      submit
                    </button>
                </div>
            </div>
        </div>
    }
</main>

when user click delete button, the row must deleted from list. and other row's submited. thanks.

Khoshtarkib
  • 170
  • 12
  • So in order word you need to delete row from db also? – Yashveer Singh Jan 21 '17 at 14:04
  • no. only remove from list. and when i submit form, removed row, not to send to acction – Khoshtarkib Jan 21 '17 at 14:06
  • plz share full html . this needs to be done using jquery . – Yashveer Singh Jan 21 '17 at 14:08
  • 1
    You need to use ajax to send the value of the object ID to a controller method which deletes the item, and in th seccess call back, remove the item from the DOM. However that means you current view code will not work because of non-consecutive collection indexers. You would need something like [this answer](http://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#comment70649447_40541892) –  Jan 21 '17 at 21:55

1 Answers1

1

You could reach that with AJAX, asyncronous call to controller action responsible for remove, or you could simply invoke the action, normal href link, that allow you to remove the item from your list.

If you are creating model by your code Visual Studio could create the controller and the proper views to edit, delete and add. To do that you just need to add a new controller, select the model name and click in ok. https://www.asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview (In VS 2015 is very similar)

Leandro
  • 114
  • 2
  • 10