【发布时间】:2016-06-15 17:19:41
【问题描述】:
我想根据 DropDownFor 中的选择将 EditorFor 中的值更改为数据库中的值。
在我的Controller 中提取值以进入 TextboxFor 的代码:
using (RexusTradingEntities RTE = new RexusTradingEntities())
{
if (PIVM.Pipe_Absolute_Roughness_Override != null || PIVM.Pipe_Absolute_Roughness_Override != 0)
{
PIVM.Pipe_Absolute_Roughness = Convert.ToDecimal(PIVM.Pipe_Absolute_Roughness_Override);
}
else
{
var PipeAbsoluteRoughness = (from P in RTE.ProjectInformations
join PM in RTE.PipeMaterials on P.Pipe_Material equals PM.Material_Name
where P.pkiProjectID == id
select PM).SingleOrDefault();
PIVM.Pipe_Absolute_Roughness = Convert.ToDecimal(PipeAbsoluteRoughness.Material_Value);
}
}
查看:
@Html.EditorFor(model => model.Pipe_Absolute_Roughness, new { htmlAttributes = new { @class = "form-control col-md-5" } })
这在第一次加载视图时有效,但我希望 EditorFor 中的值在 DropDownFor 所选值发生变化时发生变化。
当以下 EditorFor 值发生变化时,我希望同样的 EditorFor 值发生变化:
@Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @class = "form-control" } })
非常感谢您的帮助,因为我对 MVC 还是很陌生,还在学习中。
感谢您的帮助。
【问题讨论】:
-
您可以将@onchange 添加为html 属性,然后使用...让我们说javascript 来更改值。 DropDownListFor 是这样的:
@Html.DropDownListFor(model => model.selectedDropDownItem, new SelectList(Model.DropDownListInstance, "Value", "Text"), new { @class = "form-control" , @onchange="Javascript_Function()" }) -
@ShawnYan 谢谢。因此,如果我想从我的数据库中提取值,我将如何在 Javascript 函数中执行此操作?
-
据我了解,您想从数据库中提取值来填充下拉列表吗?如果有可以参考this
-
我在寻找什么,如我的帖子中所述,根据我的 DropDownListFor 中的选择,用我的数据库中的值填充 EditorFor。 @onchange 正是我需要使用的,但我仍然需要从我的数据库中提取数据,使用 javascript 甚至 json 来提取数据并传递给我的 EditorFor
标签: razor asp.net-mvc-5