İşte kod iyi çalışıyor Sorunlar, oluşturulan açılır ...
görüntüde benim görünümüdür sunucuya değer seçilebilir ancak ilanı değil
i formu gönderin
, onu sadece ilk açılan listenin değerini gönderir (tarayıcı ağında argümanları kontrol ettim), ayrıca sayfa kaynağını görüntülediğimde ajax işlevini kullanarak oluşturduğum seçenekleri göstermez. İşte benim Kod benim ilk DropDownList ikinci dropdownlist verilerine[HttpPost]
public JsonResult UpdateCountryDropDownList(int ContinentId)
{
HotelContext H = new HotelContext();
List<SelectListItem> CountryNames = new List<SelectListItem>();
List<Country> Co = H.Country.Where(x => x.ContinentId == ContinentId).ToList();
Co.ForEach(x =>
{
CountryNames.Add(new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
});
return Json(CountryNames , JsonRequestBehavior.AllowGet);
}
My Ajax çağrısı ait json dönmek
public ActionResult TwoDropDownList()
{
HotelContext H = new HotelContext();
ViewBag.DropDownListOne = new SelectList(H.Continent.ToList(), "Id", "Name");
return View();
}
Eylem oluşturmak
eylem
@model Hotel.Models.Continent
<script>
$(document).ready(function() {
$("#Name").change(function() {
var ContinentoId = $(this).val();
$.ajax({
type: "POST",
dataType: "json",
data: { ContinentId: ContinentoId },
url: '@Url.Action("UpdateCountryDropDownList","Home")',
success: function (result) {
var Country = "<select id='ddlCountry'>";
Country = Country + '<option value="">--Select--</option>';
for (var i = 0; i < result.length; i++) {
Country = Country + '<option value=' + result[i].Value + '>' + result[i].Text + '</option>';
}
Country = Country + '</select>';
$('#Countries').html(Country);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(arguments)
}
});
});
})
</script>
My Görünüm
@using(Html.BeginForm()){
SelectList se = ViewBag.DropDownListOne;
@Html.DropDownListFor(x=>x.Name,se,"--Select--")
<div id ="Countries">
@Html.DropDownList("ddlCountry",new List<SelectListItem>(),"--Select--")
</div>
<input type="submit" value="submit" style="margin-top:100px;" />
}
HttpPost Eylem
[HttpPost]
public string TwoDropDownList(string Name, string ddlCountry)
{
if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(ddlCountry))
{
return ("you must select Both");
}
else
return ("everything is working fine");
}
İyi çalıştığınız teşekkürler –
Bu hatalardan başka, ne yaptığınızın geri kalanı oldukça korkunç - bir model kullanmıyor, ViewBag kullanmıyor, güçlü bir şekilde yazılan ciltleme yok, istemci tarafı doğrulaması ve zayıf sunucu tarafı doğrulaması yok. Kodu incelemenizi tavsiye ederim. Ayrıca çalışmanızı [bu DotNetFiddle] 'da (https://dotnetfiddle.net/1bPZym) kodunuzu tavsiye edin. –