In meinem Projekt habe ich kaskadierte Dropdown-Liste mit jquery AJAX und es funktioniert, aber ich muss die zweite Dropdown-Liste auf Kontrollkästchen ändern, um die Stadt basierend auf den ausgewählten Bezirke auswählen aus der ersten Dropdown-Liste und auch die mit Checkbox ausgewählten Elemente müssen in der Datenbank gespeichert werden. Aber ich bin neu in MVC und ich bin nicht in der Lage, den Code für das Kontrollkästchen in der richtigen Weise zu geben.Änderung Dropdown-Liste zu Kontrollkästchen in AJAX JQuery in MV
Controller
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);
}
Ansicht erstellen
@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;
})
});
Ich denke, diesen Teil, das ich in jquery ändern müssen, aber ich bin nicht in der Lage
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>
zu tun
das ist mein Teil meines Codes. kann jemand bitte helfen Sie mir
Beispiel
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h4> Cascading dropdown with checkbux - Multiselect</h4>
<div class="container">
<div class="col-lg-6">
@Html.DropDownList("Country", (IEnumerable<SelectListItem>)ViewBag.Locations, "Select from", new { @class = "form-control" })
</div>
<div class="col-lg-6">
@Html.DropDownList("City", new SelectList(Enumerable.Empty<SelectListItem>()), new { @class = "multiselect", @multiple = "multiple" })
</div>
<br>
<br>
<br>
<br>
<div>
<input type="button" id="btnSubmit" value="Submit" class="btn-success" />
</div>
</div>
</body>
</html>
Referenzen unten die Lösung
Nicht klar, was Sie tun möchten.Möchten Sie für jede Stadt, die dem ausgewählten Bezirk zugeordnet ist, ein Kontrollkästchen erstellen, damit Sie mehrere Städte auswählen können? –
ja, wenn ich Bezirk wählen möchte ich Städte als Checkbox – user256