Razor 如何基于DropDownList for onChange更改EditorFor值(从DB中的值更改)?
我想根据我的DropDownFor中的选择,将EditorFor中的值更改为my DB中的值 在myRazor 如何基于DropDownList for onChange更改EditorFor值(从DB中的值更改)?,razor,asp.net-mvc-5,Razor,Asp.net Mvc 5,我想根据我的DropDownFor中的选择,将EditorFor中的值更改为my DB中的值 在my控制器中,用于将值拉入TextboxFor的代码: using (RexusTradingEntities RTE = new RexusTradingEntities()) { if (PIVM.Pipe_Absolute_Roughness_Override != null || PIVM.Pipe_Absolute_Roughness_Override
控制器中,用于将值拉入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);
}
}
视图:
这在第一次加载视图时起作用,但我希望在选定值的下拉列表更改时,EditorFor中的值也会更改
当以下EditorFor值更改时,我希望此EditorFor值更改:
@Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @class = "form-control" } })
我会很感激你的帮助,因为我对MVC还是一个新手,而且还在学习诀窍
谢谢你的帮助。已经整理好了。我将我所做的张贴在下面。请注意,我只提供我使用的特定项目,而不是所有内容(例如,我没有发布整个视图,只提供我使用的部分) 视图:
@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>
}
事实上,我甚至不需要将模型传递给控制器,我也可以将参数“val”传递给URL,但我保持原样,因为它完成了工作。我希望这对将来的其他人有所帮助。已经整理好了。我将我所做的张贴在下面。请注意,我只提供我使用的特定项目,而不是所有内容(例如,我没有发布整个视图,只提供我使用的部分)
@Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @Value=@Html.ValueFor(x=>x.Pipe_Absolute_Roughness) , @class = "form-control" } })
视图:
@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>
}
事实上,我甚至不需要将模型传递给控制器,我也可以将参数“val”传递给URL,但我保持原样,因为它完成了工作。我希望这对将来的其他人有所帮助
@Html.EditorFor(model => model.Pipe_Absolute_Roughness_Override, new { htmlAttributes = new { @Value=@Html.ValueFor(x=>x.Pipe_Absolute_Roughness) , @class = "form-control" } })
这不是一个下拉列表,但也很有用
这不是一个下拉列表,但也很有用您可以添加@onchange作为html属性,然后使用。。。让我们使用javascript来更改值。DropDownListFor应该是这样的:
@Html.DropDownListFor(model=>model.selectedDropDownItem,new SelectList(model.DropDownListInstance,“Value”,“Text”),new{@class=“form control”,“onchange=”Javascript_Function()“})
@ShawnYan谢谢。所以,如果我想从数据库中提取值,我将如何在Javascript函数中这样做?据我所知,您想从数据库中提取值来填充dropdownlist?如果是这样的话,你可以参考我在帖子中所描述的,我正在寻找的是根据我的DropDownListFor中的选择,用my DB中的值填充EditorFor。@onchange正是我需要使用的,但我仍然需要从数据库中提取数据,使用javascript甚至json提取数据并传递给我的编辑器,因为您可以将@onchange添加为html属性,然后使用。。。让我们使用javascript来更改值。DropDownListFor应该是这样的:@Html.DropDownListFor(model=>model.selectedDropDownItem,new SelectList(model.DropDownListInstance,“Value”,“Text”),new{@class=“form control”,“onchange=”Javascript_Function()“})
@ShawnYan谢谢。所以,如果我想从数据库中提取值,我将如何在Javascript函数中这样做?据我所知,您想从数据库中提取值来填充dropdownlist?如果是这样的话,你可以参考我在帖子中所描述的,我正在寻找的是根据我的DropDownListFor中的选择,用my DB中的值填充EditorFor。@onchange正是我需要使用的,但我仍然需要从数据库中提取数据,使用javascript甚至json来提取数据并传递给我的EditorFor