ASP.NET MVC 3 视图和模型
我有一个名为“组织”的模型,该模型存储在一个名为“模型”的程序集中。需要插入组织和更新组织。
有几个问题:
插入新组织时,我想确保该组织尚不存在,因此我插入了一些远程验证。然后我将模型绑定到插入视图。
现在,当我创建更新视图时,是否应该使用不同的视图模型来删除重复组织名称的远程验证?如果是这样,我无法将基本组织模型用于更新视图,那么我是否需要创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它们无效,请执行以下操作:
Key 是您视图中不正确的字段。
您应该使用相同的 ViewModel,因为在您的更新中,他们仍然可以更改“组织名称”,并且您仍然应该检查它是否是唯一的。
但为什么要使用 ViewModel 来检查验证呢?是否有理由无法检查控制器中组织名称的唯一性,并在它不唯一时执行 ModelState.AddModelError ?
ViewModel 是当您必须扩展页面时,例如
上面将是一个虚构的 DashBoardViewModel,其中我显示所有组织、员工和分配。
ViewModel 不仅仅包含一种类型的对象,它还包含多种类型。
不要忘记,有时当您需要向 View 添加一些数据时,您可以只使用 ViewData 或 ViewBag,而不是创建 ViewModel。
If their is something not valid, do this:
Key is the field in your view that is incorrect.
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
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.
对于此特定场景,验证不存在其他同名组织似乎对于插入和更新情况都有效,因此您可以重用相同的视图模型。
但是,在更新组织时验证该名称不存在必须有额外的操作,因为如果用户不更改组织名称,则数据库中至少有一条记录具有该名称(正在更新的记录),并且验证应忽略该记录。
因此,如果您选择重用视图模型,则必须根据操作(插入或更新)的上下文执行验证。
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).