C# MVC Razor两级级联列表框赢得';t滤波器
我试图实现 但当我跑步时,我不会成功。我总是出错。我希望有人能查看我的代码。我想不出错误在哪里 型号C# MVC Razor两级级联列表框赢得';t滤波器,c#,javascript,asp.net-mvc,asp.net-mvc-4,razor,C#,Javascript,Asp.net Mvc,Asp.net Mvc 4,Razor,我试图实现 但当我跑步时,我不会成功。我总是出错。我希望有人能查看我的代码。我想不出错误在哪里 型号 //LineMaintenance.cs namespace SchneiderDMS.Models { using System; using System.Collections.Generic; public partial class LineMaintenance { public int ID { get; set; } public Nullable<int
//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; }
}
}
<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>
}
//LineMaintenance.cs
名称空间SchneiderDMS.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类线路维护
{
公共int ID{get;set;}
公共可为空的LineID{get;set;}
公共可为空的开始时间{get;set;}
公共可空的AcknowleGetTime{get;set;}
公共可为null的结束时间{get;set;}
问题ID{get;set;}的公共可空类型
公共可为空的问题ID{get;set;}
公共字符串ProblemDescription{get;set;}
公共虚拟行{get;set;}
公共虚拟问题{get;set;}
公共虚拟类型问题类型问题{get;set;}
}
}
//Problem.cs
名称空间SchneiderDMS.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类问题
{
公共问题()
{
this.LineMaintenances=new HashSet();
}
公共int ID{get;set;}
问题ID{get;set;}的公共可空类型
公共字符串名称{get;set;}
公共虚拟ICollection行维护{get;set;}
公共虚拟类型问题类型问题{get;set;}
}
}
//问题的类型
名称空间SchneiderDMS.Models
{
使用制度;
使用System.Collections.Generic;
问题的公共部分类类型
{
问题()的公共类型
{
this.LineMaintenances=new HashSet();
this.Problems=newhashset();
}
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection行维护{get;set;}
公共虚拟ICollection问题{get;set;}
}
}
查看-创建.cshtml
//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; }
}
}
<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>
}
@LabelFor(model=>model.TypeOfProblemID,“TypeOfProblemID”,htmlAttributes:new{@class=“control label”,@style=“margin bottom:10px”})
@ListBox(“TypeOfProblemID”,null,htmlAttributes:new{@class=“form control”,@style=“height:150px”})
@Html.ValidationMessageFor(model=>model.TypeOfProblemID,“,new{@class=“text danger”})
@LabelFor(model=>model.ProblemID,“ProblemID”,htmlAttributes:new{@class=“control label”,@style=“margin bottom:10px”})
@ListBox(“ProblemID”,null,htmlAttributes:new{@class=“form control”,@style=“height:150px”})
@Html.ValidationMessageFor(model=>model.ProblemID,“,new{@class=“text danger”})
@节脚本{
@Scripts.Render(“~/bundles/jqueryvalc”)
$(文档).ready(函数()
{
//下拉列表Selectedchange事件
$(“#TypeOfProblemID”).change(函数()
{
$(“#ProblemID”).empty();
$.ajax({
类型:'POST',
url:“@url.Action”(“SelectProblems”),
数据类型:“json”,
数据:{id:$(“#TypeOfProblemID”).val(),
成功:功能(问题)
{
//问题包含JSON格式的列表
//从控制器传递的问题的数量
$。每个(问题,功能(i,问题)
{
$(“#ProblemID”).append(“”
+问题。名称+“”);
});
},
错误:函数(ex)
{
警报(“检索问题失败”。+ex);
}
});
返回false;
})
});
}
控制器-线路维护.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);
}
//GET:LineMaintenances/Create
公共操作结果创建()
{
ViewBag.LineID=新选择列表(db.Lines,“ID”,“Name”);
ViewBag.ProblemID=新选择列表(db.Problems,“ID”,“Name”);
ViewBag.TypeOfProblemID=新选择列表(db.TypeOfProblems,“ID”,“Name”);
返回视图();
}
//返回Json结果
公共JsonResult SelectProblems(int id)
{
db.Configuration.ProxyCreationEnabled=false;
IEnumerable Problems=db.Problems.Where(p=>p.TypeOfProblemID==id);
返回Json(问题);
}
//发布:线路维护/创建
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息请参见http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(Include=“ID,LineID,StartTime,AcknowlegeTime,EndTime,TypeOfProblemID,ProblemID,ProblemDescription”)]LineMaintenance-LineMaintenance
{
if(ModelState.IsValid)
{
db.LineMaintenances.Add(LineMaintenances);
等待db.saveChangesSync();
返回操作(“索引”);
}
ViewBag.LineID=新建选择列表(db.Lines,“ID”,“Name”,lineMaintenance.LineID);
//ViewBag.ProblemID=新建选择列表(db.Problems,“ID”,“Name”,lineMaintenance.ProblemID);
ViewBag.TypeOfProblemID=新选择列表(db.TypeOfProblems,“ID”,“Name”,lineMaintenance.TypeOfProblemID);
返回视图(线路维护);
}
在您返回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);
}
public JsonResult SelectProblems(int-id)
{
db.Configuration.ProxyCreationEnabled=false;
IEnumerable Problems=db.Problems.Where(p=>p.TypeOfProblemID==id);
返回Json(Problems,JsonRequestBehavior.AllowGet);
}