gpt4 book ai didi

c# - MVC View 中未正确删除模型子对象

转载 作者:行者123 更新时间:2023-11-30 21:59:50 24 4
gpt4 key购买 nike

我有一个基于主从模型的 MVC 主从 View 和每个详细记录的删除按钮。

按下删除按钮后,我发布要删除的 ID 和整个模型,我从模型中删除正确的详细记录并将其返回到 View 。

问题是 View 呈现不正确的详细记录。无论按下什么,它实际上都会删除最后一条记录。

模型

public class Customer
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }

public List<Contact> CustomerContacts { get; set; }
}
public class Contact
{
public int ID { get; set; }
public int CustomerID { get; set; }
public string ContactName { get; set; }
[Required]
public string ContactEmail { get; set; }
}

Controller Action

    public ActionResult Index()
{
Customer customer = new Customer(){ ID=1, Name="CustOne", Description="Descr1"};

Contact contact1 = new Contact() { ID = 1, CustomerID = 1, ContactName = "Cont_One", ContactEmail = "a@b.com" };
Contact contact2 = new Contact() { ID = 2, CustomerID = 1, ContactName = "Cont_Two", ContactEmail = "b@c.com" };
customer.CustomerContacts = new List<Contact>();
customer.CustomerContacts.Add(contact1);
customer.CustomerContacts.Add(contact2);

return View(customer);
}

[HttpPost]
public ActionResult Index(string btnSubmit, Customer customer)
{
int CustomerContactID = Convert.ToInt32(btnSubmit);

if (!ModelState.IsValid)
{
return View(customer);
}

int indexOfCust = customer.CustomerContacts.FindIndex(c => c.ID == CustomerContactID);
customer.CustomerContacts.RemoveAt(indexOfCust);

return View(customer);
}

View :

    @model MyMVCProblem.Models.Customer
@{
ViewBag.Title = "Home Page";
}

<br />
@using (Html.BeginForm())
{
<table>
<tr>
<td>Name</td>
<td>
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</td>
</tr>
<tr>
<td>Description</td>
<td>
@Html.TextAreaFor(m => m.Description)
</td>
</tr>
</table>


for (int i = 0; i < Model.CustomerContacts.Count; i++)
{
<table>
<tr>
<td>Contact Name</td>
<td>
@Html.HiddenFor(m => m.CustomerContacts[i].ID)
@Html.TextBoxFor(m => m.CustomerContacts[i].ContactName)
@Html.ValidationMessageFor(m => m.CustomerContacts[i].ContactName)
</td>
</tr>
<tr>
<td>Contact e-mail</td>
<td>
@Html.TextBoxFor(m => m.CustomerContacts[i].ContactEmail)
@Html.ValidationMessageFor(m => m.CustomerContacts[i].ContactEmail)
</td>
</tr>
</table>
<button type="submit" name="btnSubmit" id="Submit" value="@Model.CustomerContacts[i].ID">Delete</button>
<br /><br />
}
}

最佳答案

一种可能的解决方案是清除 ModelState。
示例如下:

[HttpPost]
public ActionResult Index(string btnSubmit, Customer customer)
{
int CustomerContactID = Convert.ToInt32(btnSubmit);

if (!ModelState.IsValid)
{
return View(customer);
}

int indexOfCust = customer.CustomerContacts.FindIndex(c => c.ID == CustomerContactID);
customer.CustomerContacts.RemoveAt(indexOfCust);
ModelState.Clear();
return View(customer);
}

关于c# - MVC View 中未正确删除模型子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com