で修正できますか?私のIndex.cshtml
の中には、DropDownList
にデータが入っています。 DropDownList
アイテムを選択すると、ajax
を使用して部分ビューを読み込みます。これらの部分ビューは、記入されるとデータがデータベースに挿入されるフォームです。ここでどうすればこのエラーを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");
}
:挿入した後、私は当初DropDownList
を埋めましたが、それが動作し、エラーを与えていないIndex.cshtml
に行きたいです
@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>
ここに私のモデルであり、-----です
どうすればこのエラーを修正できますか、どのようにインデックスページにリダイレクトするのかを教えてください。あなたのケースでは
たい、私はそれは 'TemplatesCategory'は' null'なので –
のあなたのコードを表示してください");'ビューをレンダリングします。 'Index()'メソッドを呼び出さないので、 'ViewBag.TemplatesCategory'は' null'です。 – Learner
私のモデルを追加したテンプレートモデル –