MVC、UpdateModel 或根据表单字段值删除?
我对此很陌生,因此非常感谢任何帮助。
我将使用晚餐/回复关系来详细说明我的问题。一顿晚餐有很多回复。 在我的晚餐编辑页面/视图上,我希望能够编辑晚餐信息和回复信息。
根据给出的答案,我有这部分工作 此处 作者: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在实际执行删除时遇到困难吗?或者是在处理表单时检测哪些应该被删除?例如哪一行不起作用:
或
?
对于#1
如果您的存储库由 Linq 2 Sql 支持并且 RSVP 是一个实体,则通常必须调用 DeleteOnSubmit() 才能从数据库中删除记录 - 只需在协会还不够。您可能会将以下内容之一添加到您的 DiningRepository 中来执行此操作:
或者,如果您希望 LINQ 自动执行删除,您可以将 DBML 编辑为 XML(右键单击,打开方式,XML 编辑器)并将以下属性添加到实体关联:
对于#2,
我通常构造这种类型的“重复实体删除复选框”表单,以便发布的值是我要删除的实体 ID 的列表。为了促进这一点,我使用备用复选框助手:
并像这样创建复选框:
并像这样使用列表:
UpdateModel() 的目的是自动将属性值从发布的表单复制到已经存在的对象上,而不是创建新的或销毁现有的实体。所以不,不是真的。这不是预期的行为。
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:
or
?
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:
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:
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:
and create the checkboxes like so:
and consume the list like so:
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.
我不太熟悉 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.