gpt4 book ai didi

c# - 我应该在 Model 还是 viewModel 中放置 "required"数据注释?

转载 作者:行者123 更新时间:2023-11-30 12:22:54 24 4
gpt4 key购买 nike

<分区>

我不确定如何设置我的数据模型。

我正在使用:MVC 5、EF 6.1.3

我有一个 Model 类,它有几个属性(装饰有几个“必需”数据注释属性以反射(reflect)创建的数据库表),这些字段是使用我的 Controller 中的 viewModel 填充的。

[HttpPost]
public ActionResult Create(CreateRequestViewModel viewModel)
{
if (!ModelState.IsValid)
{
viewModel.Affiliations = _context.Affiliations.ToList();
viewModel.Issues = _context.Issues.ToList();
return View(viewModel);
}

var request = new Request
{
RequestDate = DateTime.Today,
Status = "Open",
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
AffiliationId = viewModel.Affiliation,
IssueId = viewModel.Issue,
LastModificationDate = DateTime.Now,
RequestTypeId = 2,
};

_context.Requests.Add(request);
_context.SaveChanges();

return View("Success");
}

模型类:

public class Request
{
[Display(Name = "Request ID")]
public int Id { get; set; }

[Required]
[Display(Name = "Request Date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime RequestDate { get; set; }

[Required]
public string Status { get; set; }

[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }

[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }

[Display(Name = "Last Modified")]
public DateTime LastModificationDate { get; set; }

//navigations properties
public Affiliation Affiliation { get; set; }
public Issue Issue { get; set; }
public RequestType RequestType { get; set; }

//foreign keys
[Display(Name = "Affiliation")]
public byte AffiliationId { get; set; }
[Display(Name = "Issue")]
public int IssueId { get; set; }
[Display(Name = "Request Type")]
public byte RequestTypeId { get; set; }


}

CreateRequestViewModel 类:

public class CreateRequestViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }

public string FullName { get; set; }


public IEnumerable<Affiliation> Affiliations { get; set; }
public IEnumerable<Issue> Issues { get; set; }

[Required(ErrorMessage = "Required!")]
public byte Affiliation { get; set; }
[Required(ErrorMessage = "Required!")]
public int Issue { get; set; }
}

但是,如果我想更新反射(reflect) Model 类的数据库表上的某些字段,我必须加载所有需要的属性,然后再保存它们,因为“需要”的数据注释。问题是我只需要更新一些但不是所有属性(例如:我不会更改 FirstName 或 LastName 值)。

我的问题:我是否应该从我的模型类中删除“必需”数据注释属性并在 viewModel 上设置这些数据注释以供用户输入?如果我这样做,那么我将失去对我的数据库表上的字段的“NOT NULL”约束,但它是通过我的 modelView 强制执行的。或者我应该加载整个模型对象并再次保存所有属性,然后再对我的 dbcontext 调用 .SaveChanges() 吗?

注意:我删除了模型和 viewModel 中的几个额外属性以缩短此处的代码。

编辑;我最终实现了以下代码以避免更改模型。它似乎工作正常。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Details(DetailsViewModel request)
{

var model = new Request
{
Id = request.Id,
RequestDate = request.RequestDate,
Status = request.Status,
FirstName = request.FirstName,
LastName = request.LastName,
LastModificationDate = DateTime.Now,
AffiliationId = request.AffiliationId,
IssueId = request.IssueId,
RequestTypeId = request.RequestTypeId,

};

_context.Requests.Attach(model);
var entry = _context.Entry(model);
entry.Property(e => e.Status).IsModified = true;
entry.Property(e => e.AffiliationId).IsModified = true;
entry.Property(e => e.IssueId).IsModified = true;
entry.Property(e => e.RequestTypeId).IsModified = true;
entry.Property(e => e.LastModificationDate).IsModified = true;

_context.SaveChanges();

return RedirectToAction("Requests");

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