【问题标题】:How to change EditorFor Value(From value in DB) based on DropDownListFor onChange?如何根据 DropDownListFor onChange 更改 EditorFor 值(来自 DB 中的值)?
【发布时间】: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


【解决方案1】:

整理好了。我在下面发布我所做的。请注意,我只提供了我使用的特定项目,而不是所有内容(例如,我没有发布整个视图,只发布了我使用的部分)

查看:

@Html.DropDownListFor(model => model.Pipe_Material, Model.Pipe_MaterialList, new { @class = "form-control", id = "ddlPipe_Material", @onchange = "ChangePipeMaterialValue(this.value)" })

@Html.EditorFor(model => model.Pipe_Absolute_Roughness, new { htmlAttributes = new { @class = "form-control col-md-5", @id = "txtPipe_Absolute_Roughness" } })

@section scripts
    {
    <script src="@Url.Content("~/bootstrap.min.js")"></script>
    <script src="@Url.Content("~/jquery-2.2.3.min.js")" type="text/javascript"></script>
    <script>

        function ChangePipeMaterialValue(val) {
            //var Pipe_Material = $("#ddlPipe_Material").val();

            var ProjectDetails =
            {
                "Pipe_Material": val
            };

            $.ajax({

                url: '/ProjectInformation/GetPipeMaterialValues/',
                data: JSON.stringify(ProjectDetails),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    var m = $('#txtPipe_Absolute_Roughness');
                    m.val(data);
                }
            });
        }
    </script>
    }

控制器:

[HttpPost]
        public JsonResult GetPipeMaterialValues(ProjectInformationViewModel model)
        {
            decimal PipeMaterialValue;

            using (RexusTradingEntities RTE = new RexusTradingEntities())
            {
                    var PipeMaterialValueQuery = (from PM in RTE.PipeMaterials
                                        where PM.Material_Name == model.Pipe_Material
                                        select PM).FirstOrDefault();

                PipeMaterialValue = Convert.ToDecimal(PipeMaterialValueQuery.Material_Value);
            }

                return Json(PipeMaterialValue, JsonRequestBehavior.AllowGet);
        }

事实上,我什至不需要将模型传递给控制器​​,我也可以将参数“val”传递给 URL,但我将保持原样,因为它可以完成工作。我希望这对将来的其他人有所帮助。

【讨论】:

    【解决方案2】:
    @Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @Value=@Html.ValueFor(x=>x.Pipe_Absolute_Roughness) , @class = "form-control" } })
    

    这不是下拉菜单,但它也很有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2016-11-22
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多