简体   繁体   English

我如何在ASP.NET MVC中修复此错误

[英]How can I fix this error in asp.net mvc

Inside my Index.cshtml , I have a DropDownList populated with data. Index.cshtml ,我有一个DropDownList填充了数据。 On the selection of DropDownList item, I am loading partial views using ajax . 在选择DropDownList项目时,我正在使用ajax加载部分视图。 These partial views are forms, when filled, the data is inserted into the database. 这些局部视图是表单,填充后会将数据插入数据库中。 After the insertion, I want to go the Index.cshtml that has populated the DropDownList initially, but it doesn't work and gives the error: 插入之后,我想转到最初已填充DropDownListIndex.cshtml ,但是它不起作用并给出错误:

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'. System.Web.Mvc.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理。其他信息:没有类型为'IEnumerable'的ViewData项,其键为'TemplatesCategory'。

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

Here is my view ----- 这是我的观点-----

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

Here is my model -- 这是我的模特-

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

Please help me how i can fix this error and also, how to redirect to index page with populated dropdown. 请帮助我如何解决此错误,以及如何使用填充的下拉列表重定向到索引页。

In your case this is what could be done to call the Index method before rendering the View you want 在您的情况下,可以在呈现所需的View之前调用Index方法

return RedirectToAction("Index"); 

instead of : return View("Index"); 代替: return View("Index");

So your Clothing should be like below 所以你的衣服应该像下面

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

This will recall the Index method and render the View you want 这将调出Index方法并呈现所需的View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM