将新的Item添加到mvc.net列表中
本文关键字:mvc net 列表 添加 Item | 更新日期: 2023-09-27 18:00:38
我正试图在MVC应用程序中的现有列表中添加一个新项,我的问题是我不知道正确的方法。
到目前为止,我已经做到了:
我的客户代码:
控制器
public class CustomerController : Controller
{
public CustomerBusiness customerBusiness { get; set; }
public CustomerController()
{
customerBusiness = new CustomerBusiness();
}
//Some code that makes CRUD and more these methods
[HttpGet]
public ActionResult ViewAllJobOfferts(int id)
{
var cust = customerBusiness.GetById(id);
return View(cust.JobOfferts);
}
public ActionResult CreateJobOffert(int id)
{
var cust = customerBusiness.GetById(id);
return View(cust);
}
/* [HttpPost]
public ActionResult CreateJobOffert(JobOffertModel jobOffert)
{
return View();
}*/
}
我的实体Customer和JobOffer之间存在1到n的关系,ViewAllJobOffers方法运行良好,但当我尝试添加新的JobOffer时遇到了问题。我有几个问题,开始吧:
我必须创建一个JobOffers专用的控制器,或控制在CustomerController内部?
当我尝试创建将表单提交给new的视图时JobOffer我不知道如何将客户与这个新客户联系起来JobOffer,如果我尝试使用客户模型创建页面,我没有JobOffer属性,如果我使用JobOffer模型创建,我不知道如何在这两个对象之间建立链接。我该怎么做?
PS.:这里是两种型号的代码:
- JobOffer模型
- 客户模型
我必须创建一个JobOffers专用的控制器,或内部控件客户总监?
不一定,控制器和其他类一样应该遵循SRP(单一责任原则)。在这种情况下,只要CustomerController提供与客户相关的信息,就完全可以。
当我尝试创建将表单提交给新JobOffer的视图时,我不知道如何将客户链接到这个新JobOffert,如果我尝试使用客户模型创建页面,我没有JobOffer属性,如果我使用JobOffer模型创建,我也不知道如何在这两个对象之间建立链接。我该怎么做?
客户和JobOffer之间的链接是您定义的一对多,实体之间包含相互引用。例如,通过查询customerID=1024的JobOffer表,您可以找到Id为1024的客户的所有JobOffer。同样,每个JobOffer都可以通过实体类中的Customer引用进行跟踪。
现在,为客户创建一个新的JobOffer,这就是你可以做的:
public class CustomerController : Controller
{
public CustomerBusiness customerBusiness { get; set; }
public CustomerController()
{
customerBusiness = new CustomerBusiness();
}
//Some code that makes CRUD and more these methods
[HttpGet]
public ActionResult ViewAllJobOffersForCustomer(int customerId)
{
ICollection<JobOfferModel> model = customerBusiness.GetAllJobOffersByCustomerId(customerId);
return View(model);
}
[HttpGet]
public ActionResult CreateJobOffer()
{
// Blank model object to accept values from user,
// you may like to create a view model based on UI needs.
JobOfferModel jobOfferModel = new JobOfferModel();
return View(jobOfferModel);
}
[HttpPost]
public ActionResult CreateJobOffer(JobOfferModel jobOffer)
{
// You get a filled object here that contains customer id and job offer details
customerBusiness.CreateJobOffer(jobOffer);
return RedirectToAction("ViewAllJobOffersForCustomer", new { customerId = jobOffer.CustomerId });
}
商业服务类别示例:
public class CustomerBusiness
{
public ICollection<JobOfferModel> GetAllJobOffersByCustomerId(int customerId)
{
// TODO: Fetch job offer details from persistent store
// E.g.
// dataContext.JobOffers.Where(x => x.CustomerId == customerId).ToList();
throw new NotImplementedException();
}
public void CreateJobOffer(JobOfferModel jobOffer)
{
// TODO: Add job offer details in persistent store
// E.g.
// dataContext.JobOffers.Add(jobOffer);
}
}
修改的实体类:
public class JobOfferModel
{
[Key]
public int Id { get; set; }
public string Description { get; set; }
[Required]
[DefaultValue(false)]
public bool Acepted { get; set; }
[Required]
[DefaultValue(true)]
public bool Active { get; set; }
[Required]
[Column(TypeName = "DateTime2")]
public DateTime JobDate { get; set; }
[ForeignKey("Id")]
public int CustomerId { get; set; }
public virtual CustomerModel Customer { get; set; }
}
public class CustomerModel
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime BirthDate { get; set; }
public int PhoneNumber { get; set; }
public ICollection<JobOfferModel> JobOffert { get; set; }
}
因此,基本上您将在CustomerController中拥有一个返回空ViewModel或Model对象的方法。在视图中,您将隐藏customerId。这样,当表单发布时,它会映射到正确的客户以及JobOffer详细信息。一旦在HttpPost方法中有了模型对象,您只需要在JobOffer表(任何持久存储)中插入一个与之相关联的customerId条目
还有其他的细节,但以上典型的方法会给你一个良好的开端,我希望。干杯