.Net MVC 4 视图引擎 WebForm
在我看来,这是一个非常有趣的问题。 我有一个使用 WebForm 视图引擎的强类型视图,我不知道更改为 razor 是否可以解决我的问题。
问题: 我有一个包含汽车列表的视图,因此类型为 IList
。 我有一个弹出按钮“创建一辆新车”,弹出窗口是一个隐藏的表单,您可以调用 jQuery UI 命令$('formName').dialog()< /code> 弹出它,此表单具有可能的新车的属性,因此可能是一个具有强类型
Car
的新视图。填写表单后,应使用新车填充数据库,并应使用 Ajax 刷新汽车列表。 主要问题是我无法同时使用 HTML Helpers 来 IList
和 Car
。
简单地说: 该视图的强类型是什么?是否可以定义两个视图,另一个视图使用弹出窗口进行调用?将其更改为 Razor 可以解决我的问题吗?
最诚挚的问候,
Tito Morais
This is a quite interesting question, in my opinion.
I have a strongly typed view using the WebForm view Engine, I don't know if changing to razor would solve my problem.
PROBLEM:
I have one view with a list of cars, so of type IList <Car>
.
And I have a button "Create a new Car" that popups, the popup is a form that is hidded and you call a jQuery UI command $('formName').dialog()
to popup it, this form has the attributes of the possible new car, so probably a new view with a strongly typed Car
. After fill in the form the database should be populated with the new car, and the list of cars should be refreshed using Ajax.
The main problem is that I can't use HTML Helpers to IList <Car>
and for Car
at the same time.
Briefly: What is the strongly type for that view ? Is it possible to define two views and the other one call using pop-up? Changing it to Razor would solve my problem?
Best regards,
Tito Morais
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将列出汽车和创建新车的视图混在一起。
例如,您可以使用 jQuery 对话框或类似组件创建一个动态加载“_CreateCar”部分视图的弹出窗口。然后,当部分视图完成时,使用另一个 Ajax 调用重新加载列表视图。
Don't mix the views for listing the cars and creating a new car together.
For instance, you can make a popup that dynamically loads a "_CreateCar" partial view, using jQuery dialog or similar component. Then when the partial view is completed, reload the list view using another Ajax call.
也许不是一个优雅的解决方案是创建一个复杂的视图模型,例如:
IMO,这是正确的,因为一个视图负责列出所有汽车并创建一辆新汽车。现在,我假设您的 NewCar 具有来自控制器或其他东西的值,您需要将模型传递到视图。
@Jonas 提到的另一种方法也是正确的并且更加统一。您可以创建一个
Car
类型的分部视图_CreateCar
,使用 Jquery/Ajax 渲染它以将其加载到对话框/弹出窗口中,并将表单 POST 到Create控制器中的 (Car c)
方法。Maybe not so much an elegant solution is to create a complex view model like:
IMO this is correct since that one view is responsible for listing all cars and creating a new one. Now, I'm assuming that your
NewCar
has values coming from your controller or something, where you need to pass a model to your view.The other approach, that @Jonas mentions is also correct and more unitized. You could create a partial view
_CreateCar
with typeCar
, render it with Jquery/Ajax to load it into a dialog/popup and have the form POST to aCreate(Car c)
method in your controller.