Second DropdownList will not be Filled
I've used two Cascade DDLs on my Page.
The method of work is that when the selected item DDL1 changes, the corresponding information should be filled in DDL2 .
I used jQuery to call the DDL2 filler function:
When I select DDL1 item, the FillTypes
function is called and the corresponding records are passed to the jQuery ,But this items is not filled in DDL2 .
I do not know where is my problem?
i attached screenshot
controler:
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
model:
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
view:
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
jquery asp.net-mvc html.dropdownlistfor
add a comment |
I've used two Cascade DDLs on my Page.
The method of work is that when the selected item DDL1 changes, the corresponding information should be filled in DDL2 .
I used jQuery to call the DDL2 filler function:
When I select DDL1 item, the FillTypes
function is called and the corresponding records are passed to the jQuery ,But this items is not filled in DDL2 .
I do not know where is my problem?
i attached screenshot
controler:
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
model:
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
view:
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
jquery asp.net-mvc html.dropdownlistfor
Try removecontentType: "application/json; charset=utf-8"
and replacedata
withdata: { MachinKindId: machineid }
since you're not passing JSON string withJSON.stringify()
, but passing a value directly.
– Tetsuya Yamamoto
Jan 2 at 6:38
add a comment |
I've used two Cascade DDLs on my Page.
The method of work is that when the selected item DDL1 changes, the corresponding information should be filled in DDL2 .
I used jQuery to call the DDL2 filler function:
When I select DDL1 item, the FillTypes
function is called and the corresponding records are passed to the jQuery ,But this items is not filled in DDL2 .
I do not know where is my problem?
i attached screenshot
controler:
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
model:
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
view:
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
jquery asp.net-mvc html.dropdownlistfor
I've used two Cascade DDLs on my Page.
The method of work is that when the selected item DDL1 changes, the corresponding information should be filled in DDL2 .
I used jQuery to call the DDL2 filler function:
When I select DDL1 item, the FillTypes
function is called and the corresponding records are passed to the jQuery ,But this items is not filled in DDL2 .
I do not know where is my problem?
i attached screenshot
controler:
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
model:
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
view:
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
' GET: MachinInfo/Create
Function MachinInfoAdd() As ActionResult
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View()
End Function
' POST: MachinInfo/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function MachinInfoAdd(<Bind(Include:="Id,PelakStateID,MachinStateID,OrgCode,MachineID,BrandID,MachintypeID,NPlate,NPlateL,NPlateM,NPlateR,CardSerial,VIN,Chasis,Motor,Myear,Color,GearTypeID,MCost,Mcamp,Description")> ByVal machinInfo As MachinInfo) As ActionResult
If ModelState.IsValid Then
' If machinInfo.NPlate Is Nothing = False Then machinInfo.NPlate = machinInfo.NPlate.Replace("ایران", "").Replace(" ", "").Replace("-", "")
db.MachinInfo.Add(machinInfo)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.BrandID = New SelectList(db.Brand, "Id", "BrandName", "SelectedBrandID")
ViewBag.MachineID = New SelectList(db.MachinKind, "Id", "MachinName")
ViewBag.MachinStateID = New SelectList(db.MachinState, "Id", "Title")
ViewBag.PelakStateID = New SelectList(db.PelakState, "Id", "Title")
ViewBag.GeartypeID = New SelectList(db.GearType, "Id", "Title")
Return View(machinInfo)
End Function
Public Function FillTypes(ByVal MachinKindId As Integer) As JsonResult
Dim MachinTypes = db.MachinType.Where(Function(c) c.MachinKindId = MachinKindId).ToArray
Return Json(MachinTypes, JsonRequestBehavior.AllowGet)
End Function
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
Partial Public Class MachinType
Public Property Id As Integer
Public Property MachinTypeName As String
Public Property BrandId As Integer
Public Property MachinKindId As Integer
Public Overridable Property MachinInfo As ICollection(Of MachinInfo) = New HashSet(Of MachinInfo)
Public Overridable Property Brand As Brand
Public Overridable Property MachinKind As MachinKind
End Class
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
@ModelType Machinary.MachinInfo
@Code
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
@Using (Html.BeginForm("MachinInfoAdd", "MachinInfo", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="panel">
<div Class="form-horizontal panel-body">
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="row">
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachintypeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<select id="MachintypeID" name="MachintypeID" class="form-control"></select>
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(Function(model) model.MachineID, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("MachineID", Nothing, "انتخاب کنید", htmlAttributes:=New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(model) model.MachineID, "", New With {.class = "text-danger"})
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ُSave" class="btn btn-success" />
</div>
</div>
<br />
</div>
End Using
@section MyScripts
<script type="text/javascript">
$(function () {
$('#MachineID').change(function () {
var machineid = $(this).val();
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "FillTypes",
data: "{MachinKindId:'" + machineid + "'}",
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
});
});
</script>
End Section
jquery asp.net-mvc html.dropdownlistfor
jquery asp.net-mvc html.dropdownlistfor
edited Jan 3 at 3:47
Ashkan
asked Jan 2 at 6:36


AshkanAshkan
2215
2215
Try removecontentType: "application/json; charset=utf-8"
and replacedata
withdata: { MachinKindId: machineid }
since you're not passing JSON string withJSON.stringify()
, but passing a value directly.
– Tetsuya Yamamoto
Jan 2 at 6:38
add a comment |
Try removecontentType: "application/json; charset=utf-8"
and replacedata
withdata: { MachinKindId: machineid }
since you're not passing JSON string withJSON.stringify()
, but passing a value directly.
– Tetsuya Yamamoto
Jan 2 at 6:38
Try remove
contentType: "application/json; charset=utf-8"
and replace data
with data: { MachinKindId: machineid }
since you're not passing JSON string with JSON.stringify()
, but passing a value directly.– Tetsuya Yamamoto
Jan 2 at 6:38
Try remove
contentType: "application/json; charset=utf-8"
and replace data
with data: { MachinKindId: machineid }
since you're not passing JSON string with JSON.stringify()
, but passing a value directly.– Tetsuya Yamamoto
Jan 2 at 6:38
add a comment |
2 Answers
2
active
oldest
votes
Since you want to pass MachinKindId
value as single parameter, it's unnecessary to use contentType
setting, just pass it directly instead with GET method:
$.ajax({
type: "GET", // use GET method
dataType: "json",
url: '@Url.Action("FillTypes", "ControllerName")',
data: { MachinKindId: machineid },
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
Note:
I noticed you're not using <HttpPost()>
attribute on top of FillTypes
action and JsonRequestBehavior.AllowGet
is used, therefore the type
option must be set as GET
because by default action methods have implicit <HttpGet()>
attribute unless <HttpPost()>
attribute is specified.
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you putMachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.
– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.alert('a')
always runs butalert('b')
is never seen. And remember thatFillTypes
function will return the records. Please see my new post
– Ashkan
Jan 3 at 7:41
Check thedata
content usingconsole.log(data)
before$.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.
– Tetsuya Yamamoto
Jan 3 at 7:50
add a comment |
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs. alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
alert('a');
$.each(data, function (i, d) {
//$('#MachintypeID').append('<option>' + d.MachinTypeName + '</option>');
alert('b' );
});
},`
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002217%2fsecond-dropdownlist-will-not-be-filled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you want to pass MachinKindId
value as single parameter, it's unnecessary to use contentType
setting, just pass it directly instead with GET method:
$.ajax({
type: "GET", // use GET method
dataType: "json",
url: '@Url.Action("FillTypes", "ControllerName")',
data: { MachinKindId: machineid },
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
Note:
I noticed you're not using <HttpPost()>
attribute on top of FillTypes
action and JsonRequestBehavior.AllowGet
is used, therefore the type
option must be set as GET
because by default action methods have implicit <HttpGet()>
attribute unless <HttpPost()>
attribute is specified.
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you putMachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.
– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.alert('a')
always runs butalert('b')
is never seen. And remember thatFillTypes
function will return the records. Please see my new post
– Ashkan
Jan 3 at 7:41
Check thedata
content usingconsole.log(data)
before$.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.
– Tetsuya Yamamoto
Jan 3 at 7:50
add a comment |
Since you want to pass MachinKindId
value as single parameter, it's unnecessary to use contentType
setting, just pass it directly instead with GET method:
$.ajax({
type: "GET", // use GET method
dataType: "json",
url: '@Url.Action("FillTypes", "ControllerName")',
data: { MachinKindId: machineid },
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
Note:
I noticed you're not using <HttpPost()>
attribute on top of FillTypes
action and JsonRequestBehavior.AllowGet
is used, therefore the type
option must be set as GET
because by default action methods have implicit <HttpGet()>
attribute unless <HttpPost()>
attribute is specified.
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you putMachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.
– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.alert('a')
always runs butalert('b')
is never seen. And remember thatFillTypes
function will return the records. Please see my new post
– Ashkan
Jan 3 at 7:41
Check thedata
content usingconsole.log(data)
before$.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.
– Tetsuya Yamamoto
Jan 3 at 7:50
add a comment |
Since you want to pass MachinKindId
value as single parameter, it's unnecessary to use contentType
setting, just pass it directly instead with GET method:
$.ajax({
type: "GET", // use GET method
dataType: "json",
url: '@Url.Action("FillTypes", "ControllerName")',
data: { MachinKindId: machineid },
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
Note:
I noticed you're not using <HttpPost()>
attribute on top of FillTypes
action and JsonRequestBehavior.AllowGet
is used, therefore the type
option must be set as GET
because by default action methods have implicit <HttpGet()>
attribute unless <HttpPost()>
attribute is specified.
Since you want to pass MachinKindId
value as single parameter, it's unnecessary to use contentType
setting, just pass it directly instead with GET method:
$.ajax({
type: "GET", // use GET method
dataType: "json",
url: '@Url.Action("FillTypes", "ControllerName")',
data: { MachinKindId: machineid },
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
$.each(data, function (i, d) {
$('#MachintypeID').append('<option value=' + d.Id + '>' + d.MachinTypeName + '</option>');
});
},
failure: function (data) {
alert('error occured');
}
});
Note:
I noticed you're not using <HttpPost()>
attribute on top of FillTypes
action and JsonRequestBehavior.AllowGet
is used, therefore the type
option must be set as GET
because by default action methods have implicit <HttpGet()>
attribute unless <HttpPost()>
attribute is specified.
answered Jan 2 at 6:48
Tetsuya YamamotoTetsuya Yamamoto
17k42342
17k42342
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you putMachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.
– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.alert('a')
always runs butalert('b')
is never seen. And remember thatFillTypes
function will return the records. Please see my new post
– Ashkan
Jan 3 at 7:41
Check thedata
content usingconsole.log(data)
before$.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.
– Tetsuya Yamamoto
Jan 3 at 7:50
add a comment |
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you putMachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.
– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.alert('a')
always runs butalert('b')
is never seen. And remember thatFillTypes
function will return the records. Please see my new post
– Ashkan
Jan 3 at 7:41
Check thedata
content usingconsole.log(data)
before$.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.
– Tetsuya Yamamoto
Jan 3 at 7:50
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Thank you for your reply. Changes have been made but the problem has not been resolved. Please see the attached images.
– Ashkan
Jan 2 at 10:23
Currently I don't see where you put
MachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.– Tetsuya Yamamoto
Jan 3 at 2:10
Currently I don't see where you put
MachineID
select element, hence you should check for browser console errors. Refer to this fiddle for working example based on the answer's code.– Tetsuya Yamamoto
Jan 3 at 2:10
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.
alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records. Please see my new post– Ashkan
Jan 3 at 7:41
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs.
alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records. Please see my new post– Ashkan
Jan 3 at 7:41
Check the
data
content using console.log(data)
before $.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.– Tetsuya Yamamoto
Jan 3 at 7:50
Check the
data
content using console.log(data)
before $.each()
. If it returns an array, then your AJAX should be fine; but if nothing was returned, then you need to fix the data array.– Tetsuya Yamamoto
Jan 3 at 7:50
add a comment |
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs. alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
alert('a');
$.each(data, function (i, d) {
//$('#MachintypeID').append('<option>' + d.MachinTypeName + '</option>');
alert('b' );
});
},`
add a comment |
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs. alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
alert('a');
$.each(data, function (i, d) {
//$('#MachintypeID').append('<option>' + d.MachinTypeName + '</option>');
alert('b' );
});
},`
add a comment |
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs. alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
alert('a');
$.each(data, function (i, d) {
//$('#MachintypeID').append('<option>' + d.MachinTypeName + '</option>');
alert('b' );
});
},`
With the tests I did, I realized that the problem in the code loop section is high. Because the loop part never runs. alert('a')
always runs but alert('b')
is never seen. And remember that FillTypes
function will return the records
success: function (data) {
$('#MachintypeID').empty();
$('#MachintypeID').append('<option selected="selected" value="">Select Machintype</option>')
alert('a');
$.each(data, function (i, d) {
//$('#MachintypeID').append('<option>' + d.MachinTypeName + '</option>');
alert('b' );
});
},`
answered Jan 3 at 7:43


AshkanAshkan
2215
2215
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002217%2fsecond-dropdownlist-will-not-be-filled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try remove
contentType: "application/json; charset=utf-8"
and replacedata
withdata: { MachinKindId: machineid }
since you're not passing JSON string withJSON.stringify()
, but passing a value directly.– Tetsuya Yamamoto
Jan 2 at 6:38