我需要创建一个Trail,这条路径有子路径,子路径有水平,我有三个DropDownList。其中trail有一个下拉列表和相同的子轨道和级别。
问题是当我尝试创建新的Trail时,因为下拉列表中有一个TrailId来显示所有已存在的路径,具体取决于选择了哪条路径,运行ajax以过滤子路径并显示单个子路径,具体取决于所选路径
此视图是在创建时:
View Creating 这段代码是视图:
@model DefinityFirst.Mobile.Admin.Web.Services.Marvel.Contracts.ListTrailSubTrailLevelContract
@inject DefinityFirst.Mobile.Admin.Web.Services.Marvel.IMarvelServices DropDownDataHelper
@{
ViewBag.Title = "Create";
}@if (!ViewData.ModelState.IsValid)
{
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Warning!</strong> @Html.ValidationMessage("Error")
</div>
}
<h2>Create</h2>
<p>New TrailSubTRailLEvel</p>
@using (Html.BeginForm("TrailSubTrailLevel", "TrailSubTrailLevel", FormMethod.Post, new { id = "demoForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Trail</h4>
<div class="form-group" id="divOne">
@Html.Label("", "Trail", new { @class = "control-label col-md-2" })
<div class="col-md-10" id="partialDiv">
@Html.DropDownListFor(model => model.TrailId, await DropDownDataHelper.GetTrailsDdl(), "Select one", new { @class = "form-control", onchange = "SelectedIndexChanged()" })
@Html.ValidationMessageFor(model => model.TrailId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("", "SubTrail", new { @class = "control-label col-md-2", @name = "Subtrail", @id = "Subtrail" })
<div class="col-md-10">
@Html.DropDownList("SubTrailId", new SelectList(string.Empty, "Value", "Text"), "Please select a SubTrail", new { @class = "form-control", @id = "SubtrailId", @name = "SubtrailId" })
@Html.ValidationMessageFor(model => model.SubtrailId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("", "Level", new { @class = "control-label col-md-2", @name = "Level", @id = "Level" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.LevelId, await DropDownDataHelper.LevelsDdl(), "Select one", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LevelId, "", 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-success" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
<script type="text/javascript">
$('#SubtrailId').hide();
$('#LevelId').hide();
$("#Subtrail").hide();
$("#Level").hide();
$('#TrailId').on('change', function () {
if (this.value != 0) {
$("#Subtrail").show();
$('#SubtrailId').show();
} else {
$('#SubtrailId').hide();
$('#LevelId').hide();
}
})
$('#SubtrailId').on('change', function () {
if (this.value != 0) {
$("#Level").show();
$('#LevelId').show();
} else {
$('#LevelId').hide();
}
})
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var URL = 'http://localhost:51514/api/TrailSubTrailLevel/JSonSubTrails';
var dllDropDownSubTrail = $('#SubtrailId');
var dllDropDownTrail = $('#TrailId').val();
$(document).ready(function () {
$('#TrailId').change(function () {
$('#SubtrailId').html("");
var val = $(this).val();
$.ajax({
type: 'GET',
url: URL,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
var myData = data.Value;
localStorage.setItem('myList', myData);
$.each(myData, function (key, value) {
if (value.TrailId == $('#TrailId').val()) {
console.log(value.Name)
$('#SubtrailId').append('<option value="' + value.Id + '">' + value.Name + '</option>');
}
});
},
error: function (data) {
console.log('ERROR', data.Value);
}
});
});
});
</script>
}
这段代码是点击按钮创建的接收动作,发送所有DropDownList的信息:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ListTrailSubTrailLevelContract ListTrailSubTrailLevelContract)
{
if (ModelState.IsValid)
{
await _marvelServices.CreateTrailSubTrailLevel(ListTrailSubTrailLevelContract);
return RedirectToAction("Index", "TrailSubTrailLevel");
}
return View(ListTrailSubTrailLevelContract);
}
而且这个代码是发送属性的地方,但我需要单独使用这个属性“TrailSubTrailId”,因为在我的表中只存在两个属性“LevelId”和“TrailSubtrailId”,但是我发送了三个属性,这就是启动问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DefinityFirst.Mobile.Admin.Web.Services.Marvel.Contracts
{
public class ListTrailSubTrailLevelContract
{
public int id { get; set; }
public int TrailId { get; set; }
public string TrailName { get; set; }
public int SubtrailId { get; set; }
public string SubTrailName { get; set; }
public int LevelId { get; set; }
public string LevelName { get; set; }
public int TrailSubtrailId { get; set; }
}
}
此部分是消费服务
此代码为TrailSubTrailContract:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DefinityFirst.CarrerPath.Model.Contracts
{
public class TrailSubTrailLevelContract
{
public TrailSubTrailLevelContract()
{
//Level = new HashSet<LevelContract>();
//TrailSubtrail = new HashSet<TrailsSubTrailsListContract>();
}
public int Id { get; set; }
public int TrailSubtrailId { get; set; }
public int LevelId { get; set; }
public TrailsSubTrailsListContract TrailSubtrail { get; set; }
public LevelContract Level { get; set; }
}
}