ASP.NET MVC 中的部分视图?
我有一个相关的三个数据库表,例如:公司(一 - 一)联系人(一 - 一)地址,
我需要创建一个公司,然后为公司创建联系人,然后在一页中创建联系人地址(以使事情对用户来说更容易)。
用户不必立即填写所有详细信息并提交,他可以今天创建公司,然后明天添加联系人,然后明天编辑公司详细信息等等。这些操作是用户随机的...
有很多方法,但是使用 ASP.NET MVC 实现此目的的最佳方法是什么?
谢谢
I have a three database tables related for example: company( one - one) Contact( one- one) Address,
I need to create a company, then create contact for company, then create an address for contact in one page (to make things easier for user).
the user doenst have to fill all the details at once and submit it, he may create company today, then add contact tomorrow and then after tomorrow edit the company details and etc..the actions are randoms by the users...
there are many ways, but what is the best way to achieve this using ASP.NET MVC??
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恕我直言,最好的方法是为每个表提供一个 PartialView,并为视图提供一个将每个表对象作为属性的表单视图模型,
您将把上述模型返回到控制器中的视图
return View( FormViewModel);
然后,当您渲染每个部分时,您将传入适当的模型。
当您提交表单时,您可以执行 TryUpdateModel 来获取值并开始保存到数据层。
编辑为了回复 Robert
Sam,您还需要确保在填写数据时启用/禁用任一字段。例如,您不能先与没有公司的联系人建立联系。
如果您足够兴奋,您可以在填写数据时检查数据。因此,当用户完成表单时,您可以先激活字段而无需保存。您可以使用 jQuery 甚至可以在 MVC 中编写的客户端验证器来完成此操作。
IMHO, the best way would be to have a PartialView for each table and for the View to have a form view model that has each table object as a property
You would return the above model to the view in the controller
return View(FormViewModel);
Then when you render each partial you pass in the appropriate model.
When you submit the form you then do a TryUpdateModel to grab the values and begin saving to your data layer.
EDIT In response to Robert
Sam, you'd also need to ensure that either fields are enabled / disabled as data is filled in. For example you can't have a contact w/out a company first.
You could, if you got fired up enough, check for data as it's filled in. So as the user completes the form you activate fields w/out saving first. You can do this with jQuery and even client side validators that you can write in MVC.