Search code examples
razorasp.net-mvc-5

How to change EditorFor Value(From value in DB) based on DropDownListFor onChange?


I want to change the value in my EditorFor to a value from my DB, based on a selection from my DropDownFor.

Code to pull value to go into TextboxFor in my Controller:

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);
            }
        }

View:

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

This works when loading the View the first time, but I want the value in the EditorFor to change when ever the DropDownFor selected value changes.

I would like this same EditorFor value to change when the following EditorFor Value changes :

@Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @class = "form-control" } })

I would appreciate the help, as I am still pretty new to MVC and still learning the ropes.

Thanks for any help.


Solution

  • Got it sorted. I am posting what I did below. Please note that I am only providing the specific items I used, not everything(for example, I did not post the entire View, only the parts I used)

    View :

    @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>
        }
    

    Controller :

    [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);
            }
    

    In fact, I do not even need to pass the model through to the Controller, I can just pass the parameter "val" through to the URL as well, but I am leaving it as is, as it does the job. I hope this helps someone else in the future.