在我的項目中,我使用級聯下拉列表使用jQuery的ajax,它的工作原理,但我需要將第二個下拉列表更改爲複選框來選擇城市根據所選區從第一個下拉列表以及使用複選框選擇的項目需要保存在數據庫中。但我是MVC和Iam的新手,無法以正確的方式給出複選框的代碼。將下拉列表更改爲ajax中的複選框mvc
控制器
public ActionResult Create()
{
ViewBag.DistrictId = new SelectList(db.DistrictMasters, "Id", "DistrictName");
return View();
}
public JsonResult GetPosts(string id)
{
List<SelectListItem> posts = new List<SelectListItem>();
var postList = this.Getpost(Convert.ToInt32(id));
var postData = postList.Select(m => new SelectListItem()
{
Text = m.PostName,
Value = m.Id.ToString(),
});
return Json(postData, JsonRequestBehavior.AllowGet);
}
public IList<PostMaster> Getpost(int DistrictId)
{
return db.PostMasters.Where(m => m.DistrictID == DistrictId).ToList();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,PostId,DistrictId")] Agent agent, FormCollection formdata)
{
if (ModelState.IsValid)
{
db.Agents.Add(agent);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(agent);
}
視圖中創建
@model A.Models.Agent
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PostMaster.DistrictID, "DistrictName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", ViewBag.DistrictId as SelectList, "-- Please Select --", new { style = "width:250px" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostId, "PostName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("PostMaster", new SelectList(string.Empty, "Value", "Text"), "-- Please select --", new { style = "width:250px", @class = "dropdown1" })
</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>
}
<script type="text/javascript">
$(document).ready(function() {
//District Dropdown Selectedchange event
$("#DistrictId").change(function() {
$("#PostMaster").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetPosts")', // Calling json method
dataType: 'json',
data: { id: $("#DistrictId").val() },
// Get Selected post id.
success: function (posts) {
$.each(posts, function (i, post) {
$("#PostMaster").append('<option value="' + post.Value + '">' +
post.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve post.' + ex);
}
});
return false;
})
});
我覺得這部分,我需要在jQuery的改變,但我不能夠做到這一點
success: function (posts) {
$.each(posts, function (i, post) {
$("#PostMaster").append('<option value="' + post.Value + '">' +
post.Text + '</option>');
});
},
<div class="form-group">
@Html.LabelFor(model => model.PostId, "PostName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("PostMaster", new SelectList(string.Empty, "Value", "Text"), "-- Please select --", new { style = "width:250px", @class = "dropdown1" })
</div>
</div>
這是我的我的代碼的一部分。任何人都可以請幫我找到解決辦法
不清楚你想要做什麼。您是否想要爲與所選區域相關聯的每個城市生成一個複選框,以便您可以選擇多個城市? –
是的,當我選擇分區我想城市複選框 – user256