MVC、UpdateModel 或根据表单字段值删除?

发布于 2024-08-09 05:51:38 字数 1073 浏览 2 评论 0原文

我对此很陌生,因此非常感谢任何帮助。

我将使用晚餐/回复关系来详细说明我的问题。一顿晚餐有很多回复。 在我的晚餐编辑页面/视图上,我希望能够编辑晚餐信息和回复信息。

根据给出的答案,我有这部分工作 此处 作者:James S:

int i = 0;

foreach (var r in Dinner.RSVPs) {
  UpdateModel(r, "Dinner.RSVPs[" + i++ + "]");
}

但是,如果我想根据用户单击编辑视图上 RSVP 旁边的复选框来删除 RSVP,该怎么办?编辑视图上有类似这样的内容:

<% int i = 0;  
    foreach (var rsvp in Model.RSVPs){%>
    <%=Html.CheckBox("RemoveRSVP[" + i + "]") %>
    <%= Html.TextBox("Dinner.RSVPs[" + i + "].Name", rsvp.Name) %>
<% i++;
}%>

我尝试过这个,但显然它不起作用:

Dinner dinner = dinnerRepository.GetDinner(id);
int i = 0;

    foreach (var r in dinner.RSVPs) {
      if (Boolean.Equals(RemoveRSVP[i], true){
          dinner.RSVPs.Remove(r);
      else
           UpdateModel(r, "Dinner.RSVPs[" + i+ + "]");
      i++;
    }

我无法使用 UpdateModel 删除/删除 RSVP,可以吗?

如果有任何不清楚的地方请告诉我。

谢谢。

I'm very new to this, so any help is appreciated.

I'll use the Dinners/RSVPs relationship for detailing my problem. One Dinner has many RSVPs.
On my Dinner edit page/view I want to be able to edit the Dinner information AND the RSVPs information.

I have that part working, based on the answer given here by James S:

int i = 0;

foreach (var r in Dinner.RSVPs) {
  UpdateModel(r, "Dinner.RSVPs[" + i++ + "]");
}

But what if I want to Delete an RSVP based on the user clicking a checkbox next to the RSVP on the edit view? Something like this on the edit view:

<% int i = 0;  
    foreach (var rsvp in Model.RSVPs){%>
    <%=Html.CheckBox("RemoveRSVP[" + i + "]") %>
    <%= Html.TextBox("Dinner.RSVPs[" + i + "].Name", rsvp.Name) %>
<% i++;
}%>

I tried this, but it's not working, obviously:

Dinner dinner = dinnerRepository.GetDinner(id);
int i = 0;

    foreach (var r in dinner.RSVPs) {
      if (Boolean.Equals(RemoveRSVP[i], true){
          dinner.RSVPs.Remove(r);
      else
           UpdateModel(r, "Dinner.RSVPs[" + i+ + "]");
      i++;
    }

I can't delete/remove an RSVP using UpdateModel can I?

Let me know if anything isn't clear.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-08-16 05:51:38

我尝试过这个,但显然它不起作用:

您在实际执行删除时遇到困难吗?或者是在处理表单时检测哪些应该被删除?例如哪一行不起作用:

dinner.RSVPs.Remove(r);

if (Boolean.Equals(RemoveRSVP[i], true)

对于#1

如果您的存储库由 Linq 2 Sql 支持并且 RSVP 是一个实体,则通常必须调用 DeleteOnSubmit() 才能从数据库中删除记录 - 只需在协会还不够。您可能会将以下内容之一添加到您的 DiningRepository 中来执行此操作:

DinnerRepository.DeleteRsvp(RSVP item)
DinnerRepository.DeleteRsvp(Dinner dinner, RSVP rsvp)

或者,如果您希望 LINQ 自动执行删除,您可以将 DBML 编辑为 XML(右键单击,打开方式,XML 编辑器)并将以下属性添加到实体关联:

<Association Name="..." ... DeleteOnNull="true" />

对于#2,

我通常构造这种类型的“重复实体删除复选框”表单,以便发布的值是我要删除的实体 ID 的列表。为了促进这一点,我使用备用复选框助手:

public static class HtmlExtensions
{
    /// <summary>
    /// Alternate CheckBox helper allowing direct specification of "name", "value" and "checked" attributes.
    /// </summary>
    public static string CheckBox(this HtmlHelper html, string name, string value, bool isChecked)
    {
        string tag = String.Format("<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2}/>",
            html.AttributeEncode(name),
            html.AttributeEncode(value),
            isChecked ? "checked=\"checked\" " : ""
            );
        return tag;
    }
}

并像这样创建复选框:

<%= Html.CheckBox("RemoveRsvpIds", rsvp.RsvpId.ToString(), false) %>

并像这样使用列表:

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
}

我无法使用 UpdateModel 删除/移除 RSVP,可以吗?

UpdateModel() 的目的是自动将属性值从发布的表单复制到已经存在的对象上,而不是创建新的或销毁现有的实体。所以不,不是真的。这不是预期的行为。

I tried this, but it's not working, obviously:

Is your difficulty in actually making the delete go through? Or is it in processing the form to detect which ones should be deleted? e.g. which line doesn't work:

dinner.RSVPs.Remove(r);

or

if (Boolean.Equals(RemoveRSVP[i], true)

?

For #1

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:

DinnerRepository.DeleteRsvp(RSVP item)
DinnerRepository.DeleteRsvp(Dinner dinner, RSVP rsvp)

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:

<Association Name="..." ... DeleteOnNull="true" />

For #2

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:

public static class HtmlExtensions
{
    /// <summary>
    /// Alternate CheckBox helper allowing direct specification of "name", "value" and "checked" attributes.
    /// </summary>
    public static string CheckBox(this HtmlHelper html, string name, string value, bool isChecked)
    {
        string tag = String.Format("<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2}/>",
            html.AttributeEncode(name),
            html.AttributeEncode(value),
            isChecked ? "checked=\"checked\" " : ""
            );
        return tag;
    }
}

and create the checkboxes like so:

<%= Html.CheckBox("RemoveRsvpIds", rsvp.RsvpId.ToString(), false) %>

and consume the list like so:

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
}

I can't delete/remove an RSVP using UpdateModel can I?

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.

心不设防 2024-08-16 05:51:38

我不太熟悉 NerdDinner 代码,但他们不使用 Linq to SQL 作为后端吗?如果是这种情况,我认为您可以使用传统的 Web 方法解决此问题,并将记录 ID 附加到要删除的项目列表中每个复选框的值。然后,您可以在服务器端捕获 ID 集合,并通过将实体的选择查询传递给删除调用来执行 DeleteAllOnSubmit?如果您需要更多详细信息,请告诉我。

I am not totally familiar with the NerdDinner code but don't they use Linq to SQL for their backend? If that is the case I would think you could tackle this in a traditional web approach and append the record ID to the value of each check box in a list of items to be deleted. Then you could catch the collection of IDs on the server side and do a DeleteAllOnSubmit by passing a selection query of entities to the delete call? Let me know if you need more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文