MVC Razor两级级联ListBox过滤器
MVC Razor two level Cascading ListBox won't filter
我尝试实现http://www.devcurry.com/2013/01/aspnet-mvc-cascading-dropdown-list.html
的级联示例但是当我跑步时,我没有进入成功。我总是得到一个错误。我希望有人能帮我检查一下代码。我不知道错误在哪里。
//LineMaintenance.cs
namespace SchneiderDMS.Models
{
using System;
using System.Collections.Generic;
public partial class LineMaintenance
{
public int ID { get; set; }
public Nullable<int> LineID { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<System.DateTime> AcknowlegeTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public Nullable<int> TypeOfProblemID { get; set; }
public Nullable<int> ProblemID { get; set; }
public string ProblemDescription { get; set; }
public virtual Line Line { get; set; }
public virtual Problem Problem { get; set; }
public virtual TypeOfProblem TypeOfProblem { get; set; }
}
}
//Problem.cs
namespace SchneiderDMS.Models
{
using System;
using System.Collections.Generic;
public partial class Problem
{
public Problem()
{
this.LineMaintenances = new HashSet<LineMaintenance>();
}
public int ID { get; set; }
public Nullable<int> TypeOfProblemID { get; set; }
public string Name { get; set; }
public virtual ICollection<LineMaintenance> LineMaintenances { get; set; }
public virtual TypeOfProblem TypeOfProblem { get; set; }
}
}
//TypeOfProblem.cs
namespace SchneiderDMS.Models
{
using System;
using System.Collections.Generic;
public partial class TypeOfProblem
{
public TypeOfProblem()
{
this.LineMaintenances = new HashSet<LineMaintenance>();
this.Problems = new HashSet<Problem>();
}
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<LineMaintenance> LineMaintenances { get; set; }
public virtual ICollection<Problem> Problems { get; set; }
}
}
View - Create.cshtml
<div class="col-md-4">
@Html.LabelFor(model => model.TypeOfProblemID, "TypeOfProblemID", htmlAttributes: new { @class = "control-label", @style = "margin-bottom:10px" })
@Html.ListBox("TypeOfProblemID", null, htmlAttributes: new { @class = "form-control", @style = "height:150px" })
@Html.ValidationMessageFor(model => model.TypeOfProblemID, "", new { @class = "text-danger" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.ProblemID, "ProblemID", htmlAttributes: new { @class = "control-label", @style = "margin-bottom:10px" })
@Html.ListBox("ProblemID", null , htmlAttributes: new { @class = "form-control", @style = "height:150px" })
@Html.ValidationMessageFor(model => model.ProblemID, "", new { @class = "text-danger" })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryvalc")
<script>
$(document).ready(function ()
{
//Dropdownlist Selectedchange event
$("#TypeOfProblemID").change(function ()
{
$("#ProblemID").empty();
$.ajax({
type:'POST',
url: '@Url.Action("SelectProblems")',
dataType: 'json',
data: { id: $("#TypeOfProblemID").val() },
success: function (Problems)
{
// Problems contains the JSON formatted list
// of Problems passed from the controller
$.each(Problems, function (i, Problem)
{
$("#ProblemID").append('<option value="'
+ Problem.ID + '">'
+ Problem.Name + '</option>');
});
},
error: function (ex)
{
alert('Failed to retrieve Problems.' + ex);
}
});
return false;
})
});
</script>
}
Controller - linmaintenance .cs
// GET: LineMaintenances/Create
public ActionResult Create()
{
ViewBag.LineID = new SelectList(db.Lines, "ID", "Name");
ViewBag.ProblemID = new SelectList(db.Problems, "ID", "Name");
ViewBag.TypeOfProblemID = new SelectList(db.TypeOfProblems, "ID", "Name");
return View();
}
//Returns Json Result
public JsonResult SelectProblems(int id)
{
db.Configuration.ProxyCreationEnabled = false;
IEnumerable<Problem> Problems = db.Problems.Where(p => p.TypeOfProblemID == id);
return Json(Problems);
}
// POST: LineMaintenances/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ID,LineID,StartTime,AcknowlegeTime,EndTime,TypeOfProblemID,ProblemID,ProblemDescription")] LineMaintenance lineMaintenance)
{
if (ModelState.IsValid)
{
db.LineMaintenances.Add(lineMaintenance);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.LineID = new SelectList(db.Lines, "ID", "Name", lineMaintenance.LineID);
// ViewBag.ProblemID = new SelectList(db.Problems, "ID", "Name", lineMaintenance.ProblemID);
ViewBag.TypeOfProblemID = new SelectList(db.TypeOfProblems, "ID", "Name", lineMaintenance.TypeOfProblemID);
return View(lineMaintenance);
}
在您的代码中,当您返回json结果时,将其修改为
public JsonResult SelectProblems(int id)
{
db.Configuration.ProxyCreationEnabled = false;
IEnumerable<Problem> Problems = db.Problems.Where(p => p.TypeOfProblemID == id);
return Json(Problems,JsonRequestBehavior.AllowGet);
}
相关文章:
- 如何使用PHP和JS级联三个下拉列表
- 对级联下拉列表使用不同的连接字符串
- AngularJS或jquery中的依赖级联下拉框列表
- D3js如何在.enter上下文中在同一级别附加两个子级
- 文件结构:使用索引.js文件级联访问子模块或子文件
- 如何从一系列级联异步代码中返回值
- 我想创建4个级联下拉列表
- 带有角js的级联下拉列表
- Json级联下拉列表
- 如何级联jQuery Mobile组合框
- 在angular.js中创建级联dropbox
- JavaScript JTable-具有多个级联列表/下拉列表的Null值
- 级联和链接之间的区别是什么
- 级联下拉列表angularjs
- ASP.NET MVC级联下拉列表列出Javascript问题
- 级联下拉菜单干扰自动完成文本框
- 级联遵守形式
- 级联下拉列表mvc3,create方法
- 没有通过两个级别的JavaScript构造函数传递参数
- MVC Razor两级级联ListBox过滤器