0

我对 ASP.NET MVC 还是很陌生(或者至少在更高级的场景中使用它时)——所以我正在寻找针对特定使用场景的一些反馈以及实现它的最佳方法。我尝试进行了多次搜索 - 但没有找到类似的东西。

我对实现它的方法很灵活——只要提议的解决方案运行良好。

比如说,我有一个名为 的实体Vehicle,它与一个实体相关联,VehicleType并且Vehicle还与一个属性实体相关联,具体基于被VehicleType调用的VehicleProperties。例如 - 车辆类型可以是“Car”、“Boat”等,VehicleProperties 包含 Car VehicleType 的“BodyStyle”、“Color”等以及“Boat”VehicleType 的“Length”、“Weight”等

On the create and edit forms for the VehicleConntrollerI'm looking to do something along these lines: Have a drop down list of vehicle types and when the selection of that changes load the appropriate "VehicleProperties" form that is specific to this VehicleType.

这就是我的问题所在 - MVC 中最好的方法是什么?寻找 AJAX 类型的方法。如果有人有任何示例教程或文章可以概述潜在的解决方案 - 我将不胜感激!

我目前正在使用带有 jQ​​uery 的 ASP.NET MVC 5 - 但如果需要一个优雅的解决方案,我愿意添加其他 JS 库。

4

1 回答 1

0

解决方法在这里,

1.创建一个强类型视图并为返回VehicleType集合的视图创建一个动作方法,即List < VehicleType >

2.因此,您可以通过使用 html 标记迭代模型来在视图中创建 DropDownlist。在您的情况下,模型是“List < VehicleType >”

3.创建一个强类型的局部视图,VehicleProperties 类型。

4.创建另一个带有参数的操作方法,该方法将返回具有适当车辆属性的局部视图;

5.为DropDownlist写一个onchange java脚本函数。通过 jQuery Ajax 调用 action 方法,并将选定的下拉值作为参数。

6. ajax 调用的响应将是 html 内容,您可以将其附加到 html div。

完毕..!!快乐编码..!!

于 2014-07-22T08:56:01.530 回答