如何在MVC 4中通过Javascript选择ddl Id [英] How to get selected ddl Id through Javascript in MVC 4
问题描述
亲爱的所有,
我想相应地设置联系信息的值我有一个ddl,一旦我从ddl选择了特定的项目,那么我想要设置其余的全部使用javascript的联系信息领域。
我无法找到ddl的选定项目值如何使用javascript找到它我的代码如下。
即使有任何很好的方式来实现这种情况然后请告诉我。
我的ddl在这里..
Dear All,
I want to set value of contact information accordingly I have a ddl, once I select perticular item from ddl then I want set rest of all the field of contact information using javascript.
I am not able to find ddl's selected items value how I can find it using javascript my code is below.
even is there any great way to achieve this kind of scenario then plz let me know.
my ddl is here..
@Html.DropDownListFor(model => model.leadDetailModels.Id, new SelectList(Model.contactNames, "Id", "Name"), "-Select ContactName-", new {id = "ddlContactNames", @style = "width: 310px;height : 30px" })
现在这个模型=> model.leadDetailModel.Id正在设置但是我无法在intl javascript中找到这个值。
我的javascript代码就像这里...
now this model=>model.leadDetailModel.Id is being set but I am not able to find this value intl javascript.
my javascript code is like here...
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function () {
$("select#ddlContactNames").change(function () {
$("#txtContactName").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
$("#txtContactPhone").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
$("#txtContactOtherPhone").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
$("#txtContactEmail").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
$("#txtContactArea").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
$("#txtContactComments").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
});
});
</script>
}
以上mode.leadDetailModels.Id的粗体值设置为null所以我在这里得到例外...
我怎么解决这个问题..?
谢谢..
above bold value of "mode.leadDetailModels.Id" is set null so I am getting exception here...
how I can resolve this..?
thank you..
推荐答案
(function(){
(function () {
( select#ddlContactNames)。change(function(){
("select#ddlContactNames").change(function () {
( < span class =code-string> #txtContactName)。val(@ Html.Raw(Json.Encode(@ Model.contactNames [model.leadDetailModels.Id] .Name)));
("#txtContactName").val(@Html.Raw(Json.Encode(@Model.contactNames[model.leadDetailModels.Id].Name)));
这篇关于如何在MVC 4中通过Javascript选择ddl Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!