Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I tried this, but it's not working, obviously:</p> </blockquote> <p>Is your difficulty in actually making the delete go through? Or is it in processing the form to detect which ones <em>should</em> be deleted? e.g. which line doesn't work:</p> <pre><code>dinner.RSVPs.Remove(r); </code></pre> <p>or</p> <pre><code>if (Boolean.Equals(RemoveRSVP[i], true) </code></pre> <p>?</p> <h2>For #1</h2> <p>If your repository is backed by Linq 2 Sql and RSVP is an entity, you will usually have to cause DeleteOnSubmit() to be called in order for the record to be deleted from the database--just calling Remove() on the association will not be enough. You probably will add one of the following to your DinnerRepository to do this:</p> <pre><code>DinnerRepository.DeleteRsvp(RSVP item) DinnerRepository.DeleteRsvp(Dinner dinner, RSVP rsvp) </code></pre> <p>Alternately, if you want LINQ to perform the delete automatically, you can edit the DBML as XML (right click, open with, XML Editor) and add the following attribute to the entity association:</p> <pre><code>&lt;Association Name="..." ... DeleteOnNull="true" /&gt; </code></pre> <h2>For #2</h2> <p>I usually construct this type of "repeating entity-delete checkbox" form so that the posted values are a list of the entity IDs I want to delete. To facilitate this I use an alternate CheckBox helper:</p> <pre><code>public static class HtmlExtensions { /// &lt;summary&gt; /// Alternate CheckBox helper allowing direct specification of "name", "value" and "checked" attributes. /// &lt;/summary&gt; public static string CheckBox(this HtmlHelper html, string name, string value, bool isChecked) { string tag = String.Format("&lt;input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2}/&gt;", html.AttributeEncode(name), html.AttributeEncode(value), isChecked ? "checked=\"checked\" " : "" ); return tag; } } </code></pre> <p>and create the checkboxes like so:</p> <pre><code>&lt;%= Html.CheckBox("RemoveRsvpIds", rsvp.RsvpId.ToString(), false) %&gt; </code></pre> <p>and consume the list like so:</p> <pre><code>public ActionResult TheFormInQuestion(int dinnerId, int[] removeRsvpIds) { var dinner = DinnerRepository.GetDinner(dinnerId); foreach (var rsvp in dinner.RSVPs) { if (removeRsvpIds.Contains(rsvp.RsvpId)) { // Perform Delete } else { // Perform Update } } // The rest } </code></pre> <blockquote> <p>I can't delete/remove an RSVP using UpdateModel can I?</p> </blockquote> <p>The purpose of UpdateModel() is to automagically copy property values from the posted form onto an already-existing object--not to create new or destroy existing entities. So no, not really. It wouldn't be the expected behavior.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload