在我的Index.cshtml
里面,我有一个填充了数据的DropDownList
。在选择DropDownList
项目时,我使用ajax
加载部分视图。这些部分视图是表单,填充后,数据将被插入到数据库中。在插入之后,我想去已填充DropDownList
最初Index.cshtml
,但它不工作,并给出了错误:如何解决这个错误在asp.net mvc
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'TemplatesCategory'.
这里是我的控制器----
public class templateController : Controller
{
PulseContext db = new PulseContext();
public ActionResult Index()
{
ViewBag.TemplatesCategory = new SelectList(db.Templates, "Id", "templateName");
return View();
}
[HttpGet]
public PartialViewResult Clothing()
{
return PartialView("_ClothingTemplate");
}
[HttpPost]
public ActionResult Clothing(templateClothing tc)
{
db.templateClothings.Add(tc);
db.SaveChanges();
return View("Index");
}
这是我的看法-----
@model Pulse.Models.Templates
@{
ViewBag.Title = "Index";
}
<div class="container">
<script src="~/Scripts/jquery-2.2.3.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<h2>Select your Template</h2>
@Html.DropDownList("TemplatesCategory")
<script type="text/javascript">
$(document).ready(function() {
$("#TemplatesCategory").change(function() {
var choice = this.value;
if (choice === "1") {
$.ajax({
url: '/template/Footwear',
contentType: 'application/html;charset=utf-8',
type: 'GET',
datatype:'html'
})
.success(function(result){
$('#templateContainer').html(result)
})
}
else if (choice === "2") {
$.ajax({
url: '/template/Accessory',
contentType: 'application/html;charset=utf-8',
type: 'GET',
datatype: 'html'
})
.success(function (result) {
$('#templateContainer').html(result)
})
}
else if (choice === "3") {
$.ajax({
url: '/template/Clothing',
contentType: 'application/html;charset=utf-8',
type: 'GET',
datatype: 'html'
})
.success(function (result) {
$('#templateContainer').html(result)
})
}
});
});
</script>
<div id="templateContainer"></div>
</div>
这里是我的模型 -
namespace Pulse.Models
{
using System;
using System.Collections.Generic;
public partial class Templates
{
public Templates()
{
this.templateAccessoriesTbls = new HashSet<templateAccessories>();
this.templateClothingTbls = new HashSet<templateClothing>();
this.templateFootwearTbls = new HashSet<templateFootwear>();
}
public int Id { get; set; }
public string templateName { get; set; }
public virtual ICollection<templateAccessories> templateAccessoriesTbls { get; set; }
public virtual ICollection<templateClothing> templateClothingTbls { get; set; }
public virtual ICollection<templateFootwear> templateFootwearTbls { get; set; }
}
}
请帮助我如何解决这个错误,以及如何重定向到带有填充下拉列表的索引页面。
请出示你的我已经加入我的模型模板模型 –
的代码 – Learner
这意味着'TemplatesCategory'是'null' –