0

In meinem Index.cshtml, habe ich eine DropDownList mit Daten bestückt. Bei der Auswahl von DropDownList Artikel lade ich Teilansichten mit ajax. Diese Teilansichten sind Formulare. Wenn sie ausgefüllt sind, werden die Daten in die Datenbank eingefügt. Nach dem Einsetzen möchte ich die Index.cshtml gehen, die die DropDownList zunächst bevölkert hat, aber es funktioniert nicht und gibt den Fehler:Wie kann ich diesen Fehler in asp.net mvc beheben

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'.

Hier ist mein Controller ----

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"); 
     } 

hier ist meine Ansicht ist -----

@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> 

hier ist mein Modell -

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; } 
    } 
} 

Bitte helfen Sie mir, wie ich diesen Fehler beheben kann und auch, um zur Indexseite mit bevölkerten Dropdown-Liste umleiten.

+0

zeigen Bitte Code des Templates Modell –

+0

ich mein Modell hinzugefügt haben – Learner

+0

Es bedeutet, dass '' TemplatesCategory' ist null' –

Antwort

0

In Ihrem Fall ist es das, was die Index Methode aufzurufen getan werden könnte, vor dem Rendern der View Sie

return RedirectToAction("Index"); 

statt: return View("Index");

So Ihre Kleidung sollte wie unten

[HttpPost] 
    public ActionResult Clothing(templateClothing tc) 
    { 
     db.templateClothings.Add(tc); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

Dies ruft die Index Methode zurück und rendern die View Sie wollen

Verwandte Themen