ASP.NET MVC 3 视图和模型

发布于 2024-12-14 17:39:55 字数 276 浏览 2 评论 0原文

我有一个名为“组织”的模型,该模型存储在一个名为“模型”的程序集中。需要插入组织和更新组织。

有几个问题:

插入新组织时,我想确保该组织尚不存在,因此我插入了一些远程验证。然后我将模型绑定到插入视图。

现在,当我创建更新视图时,是否应该使用不同的视图模型来删除重复组织名称的远程验证?如果是这样,我无法将基本组织模型用于更新视图,那么我是否需要创建 2 个不同的视图,一个用于插入,一个用于更新?如果是这种情况,就会有两个基本相同但只是使用不同模型的视图。

有人可以帮忙吗?

I have a model, called Organisation and the model is stored in an assembly called Model. There is a requirement to insert an organisation and update an organisation.

Couple of questions:

When inserting a new organisation, I want to ensure that the organisation doesn't already exist so I've inserted some remote validation. I then bind the model to the insert view.

Now, when I'm creating the update view should I use a different view model which removes the remote validation for duplicate organisation names? If so, I can't use my base Organisation model for the update view, so do I then need to create 2 different views, one for insert and one for update? If this is the case, there is going to be 2 views that are basically the same but just use different models.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-12-21 17:40:01
  • 问题 1:检查验证:

如果它们无效,请执行以下操作:

If(isNotValid()){
    ModelState.AddModelError("Key", "The user name or password provided is incorrect.")
}

Key 是您视图中不正确的字段。

  • 问题 2:创建/编辑之间的差异

您应该使用相同的 ViewModel,因为在您的更新中,他们仍然可以更改“组织名称”,并且您仍然应该检查它是否是唯一的。

但为什么要使用 ViewModel 来检查验证呢?是否有理由无法检查控制器中组织名称的唯一性,并在它不唯一时执行 ModelState.AddModelError ?

ViewModel 是当您必须扩展页面时,例如

  public class DashBoardViewModel
  { public List(Of Organisation) Organisation {get;set;}
    public List(Of Staff) Staff{get;set;}
    public List(Of Assignment) Assignments{get;set;}
  }

上面将是一个虚构的 DashBoardViewModel,其中我显示所有组织、员工和分配。
ViewModel 不仅仅包含一种类型的对象,它还包含多种类型。

不要忘记,有时当您需要向 View 添加一些数据时,您可以只使用 ViewData 或 ViewBag,而不是创建 ViewModel。

  • Question 1: check validation:

If their is something not valid, do this:

If(isNotValid()){
    ModelState.AddModelError("Key", "The user name or password provided is incorrect.")
}

Key is the field in your view that is incorrect.

  • Question 2: Difference between Create / Edit

You should use the same ViewModel, because in your update, they can still change the "Organisation Name" and you should still check if it is unique.

But why should you use a ViewModel just to check the validation? Is there a reason why you can't check the organisation names for uniqueness in your controller and do a ModelState.AddModelError when it is not unique?

A ViewModel is when you have to extend the Page, For Example

  public class DashBoardViewModel
  { public List(Of Organisation) Organisation {get;set;}
    public List(Of Staff) Staff{get;set;}
    public List(Of Assignment) Assignments{get;set;}
  }

Above would be a fictional DashBoardViewModel where i show all the Organisations, Staff and Assignments.
A ViewModel doesn't contain just one type of object, it contains multiple.

And don't forget, sometimes when you need to add some data to a View, you could just use ViewData or ViewBag, instead of creating a ViewModel.

命硬 2024-12-21 17:40:00

对于此特定场景,验证不存在其他同名组织似乎对于插入和更新情况都有效,因此您可以重用相同的视图模型。

但是,在更新组织时验证该名称不存在必须有额外的操作,因为如果用户不更改组织名称,则数据库中至少有一条记录具有该名称(正在更新的记录),并且验证应忽略该记录。

因此,如果您选择重用视图模型,则必须根据操作(插入或更新)的上下文执行验证。

For this specific scenario the validation that no other organization with the same name exists seems valid for both the insert and update case so you could reuse the same view model.

However validating that the name does not exists when updating a organization must have extra because if the user does not change the organization name then at least one record on the database has that name, the one being updated, and the validation should ignore that record.

So if you choose to reuse the view model the validation must perform according to the context of the operation (insert or update).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文