我正在使用 ASP.NET Core MVC 和 TagHelpers。
我的项目有名为“GroupEntity”、“CustomerEntity”和“GroupsCustomerEntity”的实体。
我想通过“CustomerController”的“编辑”操作将客户添加到组中。在客户的“表单”视图中,每个组都应该有一个复选框。
我尝试了网上找到的所有方法,但无法做到。所以我只是发布我的控制器并请你告诉我我应该做什么。这是 github 链接:github
public class CustomerEntity : BaseEntity
{
public string FirstName { get; set; }
public string? LastName { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
public string? City { get; set; }
public string? Interest { get; set; }
public string? FindHow { get; set; }
public string? Note { get; set; }
//relational prop
public List<PersonalSessionEntity> PersonalSessions { get; set; }
public List<GroupsCustomersEntity> Groups { get; set; }
}
public class GroupEntity : BaseEntity
{
public int UserId { get; set; } //User is the coach of this group.
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public double Price { get; set; }
public string Note { get; set; }
//relational prop
public UserEntity User { get; set; }
public List<GroupsCustomersEntity> Customers { get; set; }
}
public class GroupsCustomersEntity : BaseEntity
{
public int GroupId { get; set; }
public int CustomerId { get; set; }
//relational prop
public GroupEntity Group { get; set; }
public CustomerEntity Customer { get; set; }
}
我很确定我的表格是正确的。这是我在 CustomerController 中的更新和保存操作。
[HttpGet]
public ActionResult Update(int id)
{
var updateCustomerDto = _customerService.GetCustomer(id);
var viewModel = new CustomerFormViewModel()
{
FirstName = updateCustomerDto.FirstName,
LastName = updateCustomerDto.LastName,
Phone = updateCustomerDto.Phone,
Email = updateCustomerDto.Email,
City = updateCustomerDto.City,
FindHow = updateCustomerDto.FindHow,
Interest = updateCustomerDto.Interest,
Note = updateCustomerDto.Note,
};
return View("Form", viewModel);
}
[HttpPost]
public IActionResult Save(CustomerFormViewModel formData)
{
if (ModelState.IsValid)
{
if (formData.Id == 0)
{
var addCustomerDto = new AddCustomerDto()
{
FirstName = formData.FirstName,
LastName = formData.LastName,
Phone = formData.Phone,
Note = formData.Note,
City = formData.City,
Interest = formData.Interest,
Email = formData.Email,
FindHow = formData.FindHow,
};
var result = _customerService.AddCustomer(addCustomerDto);
if (result)
{
return RedirectToAction("List");
}
else
{
ViewBag.ErrorMessage = "This customer already exists.";
return View("form", formData);
}
}
else //update
{
var updateCustomerDto = new UpdateCustomerDto()
{
Id = formData.Id,
FirstName = formData.FirstName,
LastName = formData.LastName,
Phone = formData.Phone,
Interest = formData.Interest,
City = formData.City,
Note = formData.Note,
Email = formData.Email,
FindHow = formData.FindHow,
};
_customerService.UpdateCustomer(updateCustomerDto);
return RedirectToAction("List");
}
}
return View("form", formData);
}
最后,这是我的观点。我正在使用 aspTagHelpers,但我还没有看到有人使用它。
@model CustomerFormViewModel
<div class="row">
<div class="col-8 offset-2">
<div class="card" style="border:solid 2px">
<div class="card-header fw-bold fs-5">
Müşteri Formu
</div>
<div class="card-body">
<form method="post" asp-area="Admin" asp-controller="Customer" asp-action="Save">
@if (!string.IsNullOrWhiteSpace(ViewBag.ErrorMessage))
{
<div class="alert alert-danger">
@ViewBag.ErrorMessage
</div>
}
<input asp-for="Id" type="hidden" />
<div class="mb-2">
<label asp-for="FirstName"></label>
<input asp-for="FirstName" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="FirstName"></span>
</div>
<div class="mb-2">
<label asp-for="LastName"></label>
<input asp-for="LastName" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="LastName"></span>
</div>
<div class="mb-2">
<label asp-for="Phone"></label>
<input asp-for="Phone" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="Phone"></span>
</div>
<div class="mb-2">
<label asp-for="Email"></label>
<input asp-for="Email" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="Email"></span>
</div>
<div class="mb-2">
<label asp-for="City"></label>
<input asp-for="City" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="City"></span></div>
<div class="mb-2">
<label asp-for="Groups"></label>
@for (int i = 0; i < Model.Groups.Count(); i++)
{
<input asp-for="Groups" />
}
</div>
<div class="mb-2">
<label asp-for="FindHow"></label>
<input asp-for="FindHow" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="FindHow"></span>
</div>
<div class="mb-2">
<label asp-for="Interest"></label>
<input asp-for="Interest" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="Interest"></span>
</div>
<div class="mb-2">
<label asp-for="Note"></label>
<input asp-for="Note" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="Note"></span>
</div>
<button type="submit" class="btn btn-success mt-3 float-end">Send</button>
</form>
</div>
</div>
</div>
</div>
我尝试遵循this教程。但我不明白他的视图页面或他的 linq 查询。
并且 this 教程有一个用于连接表的控制器,这感觉不对。
有时 taghepler 不能满足您的要求,您需要手动编写 name 属性,我创建一个简单的演示来展示如何使用复选框来更新多对多关系中的相关模型。希望可以帮助您解决这个问题。
型号
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public List<Customer> customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public List<Group> groups { get; set; } = new List<Group>();
}
public class CustomerViewModel
{
public Customer customer { get; set; }
public List<Group> groups { get; set; }
}
后端
[HttpGet]
public IActionResult Edit(int id)
{
var customer = _dbcontext.customers.Include(x => x.groups).Where(x => x.Id == id).FirstOrDefault();
var groups = _dbcontext.groups.ToList();
CustomerViewModel customerView = new CustomerViewModel();
customerView.customer = customer;
customerView.groups = groups;
return View(customerView);
}
[HttpPost]
public IActionResult Edit(CustomerViewModel model, int[] groupIds)
{
var customer = _dbcontext.customers
.Include(c => c.groups) // Load associated groups
.FirstOrDefault(x => x.Id == model.customer.Id);
if (customer != null)
{
// Update customer properties
customer.Name = model.customer.Name;
customer.Age = model.customer.Age;
// Clear existing group associations
customer.groups.Clear();
if (groupIds != null)
{
foreach (var groupId in groupIds)
{
var group = _dbcontext.groups.Find(groupId);
if (group != null)
{
customer.groups.Add(group);
}
}
}
_dbcontext.Update(customer);
_dbcontext.SaveChanges();
}
return RedirectToAction("edit");
}
前端
@model CustomerViewModel
@{
int index = 0;
}
<form method="post" asp-action="Edit">
<input type="hidden" asp-for="customer.Id" />
<div class="mb-2">
<label asp-for="customer.Name"></label>
<input asp-for="customer.Name" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="customer.Name"></span>
</div>
<div class="mb-2">
<label asp-for="customer.Age"></label>
<input asp-for="customer.Age" type="text" class="form-control" />
<span class="text-danger fw-bold mt-1" asp-validation-for="customer.Age"></span>
</div>
@foreach (var group in Model.groups)
{
<input type="checkbox" name="groupIds" value="@group.Id"
@(Model.customer.groups.Contains(group) ? "checked" : "")>
<label>@group.Name</label>
}
<button>Edit</button>
</form>
gif 演示