MVCモデルにリンクするときに依存リストを取得する際に問題が発生しています。他の回答では静的リストを使用しています(私は働くことができます)が、実際のデータにリンクするためのこの最後のステップはありません。以下のコードを実行すると、何も起こりません。それを推測マイモデルにリンクされたMVC依存リスト(静的リストではない)
は私が(警告「SO FAR WORKS」右下の)リストを設定するために使用するJavaScriptの一部で必要な1つのシンプルなラインです。または、私の新しい選択リストでdropdownlistforを更新する方法があるはずです(各メソッドを使用して1つずつプッシュするのではなく)。
コントローラー:
public class EMPLOYEEsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Create()
{
ViewBag.STATEID = new SelectList(db.STATEs, "STATEID", "STATENAME");
ViewBag.CITYID = new SelectList(db.CITYs, "CITYID", "CITYNAME");
return View();
}
[HttpGet]
public ActionResult UpdateCity(string id)
{
SelectList CityList = new SelectList(db.CITYs.Where(x => x.STATEID.Equals(id)), "CITYID", "CITYNAME");
return Json(CityList, JsonRequestBehavior.AllowGet);
}
}
ビュー:
@model myproj.Models.EMPLOYEE
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<script type="text/javascript" src="/scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
$('#STATEID').change(function() {
$.ajax({
url: '@Url.Action("UpdateCity")',
type: 'GET',
dataType: 'json',
cache: false,
data: { id: $(this).val() },
success: function (CityList) {
alert('WORKS SO FAR');
var citySelect = $('#CITYID');
citySelect.empty();
$.each(CityList, function (index, locations) {
citySelect.append(
$('<option/>')
.attr('value', CityList.CITYID)
.text(CityList.CITYNAME)
);
});
},
error: function() { alert('ERROR'); }
});
});
});
</script>
<div class="form-horizontal">
<h4>EMPLOYEE</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EMPLOYEENAME, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EMPLOYEENAME, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EMPLOYEENAME, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.STATEID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.STATEID, (IEnumerable<SelectListItem>)ViewBag.STATEs, "Select State", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.STATEID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CITYID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CITYID, (IEnumerable<SelectListItem>)ViewBag.CITYs, "Select City", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CITYID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
{にconsole.log(CityList、インデックス、場所);}'あなたは何を観察するかは – Jasen
マイCityListがコンソールによさそうだ[オブジェクト、オブジェクト、?。 Object、Object]これは、適切なテキストと値を持つ4つの詳細なオブジェクトを持っています –
私は 'ViewBag.STATEID = ...'と仮定し、 'ViewBag.CITYID = ...'はタイプミスです。 'ViewBag.STATEs = ...'と 'ViewBag.CITYs = ...'そうでなければ他の問題があります。 –