0

で修正できますか?私の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> 

ここに私のモデルであり、-----です

どうすればこのエラーを修正できますか、どのようにインデックスページにリダイレクトするのかを教えてください。あなたのケースでは

+0

たい、私はそれは 'TemplatesCategory'は' null'なので –

+0

のあなたのコードを表示してください");'ビューをレンダリングします。 'Index()'メソッドを呼び出さないので、 'ViewBag.TemplatesCategory'は' null'です。 – Learner

+0

私のモデルを追加したテンプレートモデル –

答えて

0

これはViewあなたが

return RedirectToAction("Index"); 

代わりにしたいレンダリングする前にIndexメソッドを呼び出すために何ができるかです:return View("Index");

をだからあなたの服は

[HttpPost] 
    public ActionResult Clothing(templateClothing tc) 
    { 
     db.templateClothings.Add(tc); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
以下のようにする必要がありますが

これはIndexメソッドを呼び出して、View

関連する問題