0

我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。

我也在使用实体框架和代码优先方法。

我有一个包含 PartialView 的视图。这意味着当我单击视图中的按钮时,会出现部分视图。

视图 (Index.aspx) 和部分视图 (Gestion.ascx) 包含 DropDownList 和 TextBox 以填充将保存在我的基础中的表“Gamme”中的值。

这是视图“Index.aspx”:

<% using (Html.BeginForm("Create", "Anouar"))
   { %>
  <div><%:Html.Label("Gamme :")%><%: Html.DropDownList("SelectedProfile_Ga", new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"))%> <input type="button" value="Configurer" id="btnShowGestion" /></div> 




<div id="divGestion"><%: Html.Partial("Gestion", Model) %></div>
       <% } %>   
        <script type="text/javascript">

            $(document).ready(function () {

                //                $('#divGestion').load('/Anouar/Gestion');



                $('#btnShowGestion').click(function () { $('#divGestion').slideToggle("slow") });



            });

</script>

</asp:Content>

这是填充视图“索引”的控制器:

public class ProfileGaController : Controller
    {
        private GammeContext db = new GammeContext();

        //
        // GET: /ProfileGa/
        [HttpGet]
        public ActionResult Index(Profile_Ga profile_ga, Poste poste)
        {

            var viewModel = new FlowViewModel();
            viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste"); 
            //viewModel.PostesItems = db.Postes.ToList() ?? new List<Poste>();
               viewModel.Profile_GaItems = db.Profil_Gas.ToList();
               viewModel.GaItems = db.Gammes.ToList();

            return View(viewModel);



        }

这是部分视图“Gestion.ascx”:

<fieldset class="parametrage">
        <legend>Gestion de Gamme</legend>

        <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div>


         <div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div>
        <div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div>
        <div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div>
        <div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div>
        <div><input type="submit" value="Enregistrer" id="btnSave"  /></div>

        </fieldset>

这是填充 PartialView 的控制器:

public class AnouarController : Controller
    {

         private GammeContext db = new GammeContext();


        //
        // GET: /Anouar/

         public ActionResult Gestion(FlowViewModel model)
         {

             model.YourGammeModel = new Gamme();
             return PartialView(model);

         }

         [HttpPost]
         public ActionResult Create(FlowViewModel model)
         {
        if (ModelState.IsValid)
            {

                db.Gammes.Add(model.YourGammeModel);
                db.SaveChanges();
                return RedirectToAction("Gestion");  
            }


            return View(model.YourGammeModel);
        }
    }

最后这是包含属性的 ViewModel :

public class FlowViewModel
    {

        [Key]
        public string IDv { get; set; }
        [NotMapped]
        public SelectList PostesItems { get; set; }

        public List<Profile_Ga> Profile_GaItems { get; set; }
        public List<Gamme> GaItems { get; set; }

        public Gamme YourGammeModel { get; set; }

        public int SelectedProfile_Ga { get; set; }

        public int SelectedGamme{ get; set; }

        public int SelectedPoste { get; set; }

        public int PostePrecedentSelected { get; set; } 
        public int PosteSuivantSelected { get; set; }        
    }

当我执行我的代码时,总是出现以下错误:

未找到“创建”视图或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Anouar/Create.aspx ~/Views/Anouar/Create.ascx ~/Views/Shared/Create.aspx ~/Views/Shared/Create.ascx ~/Views/Anouar/Create。 cshtml ~/Views/Anouar/Create.vbhtml ~/Views/Shared/Create.cshtml ~/Views/Shared/Create.vbhtml

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

这是我在函数 Create 处设置断点时有关模型值的屏幕截图: 图片

4

1 回答 1

2

首先,你得到的错误是因为ModelState.IsValid是假的,所以它正在命中

return View(model.YourGammeModel);

由于它在一个名为Create您没有指定要返回的视图的方法中,它正在寻找一个名为 Create 的视图,但没有找到导致错误的视图。

您需要指定要返回的视图或创建一个名为 Create 的视图以清除此错误。然后你需要调查为什么ModelState.IsValid是假的。

编辑:再看一遍,我怀疑这ModelState是无效的,因为你有这个[Key]属性IDv并且它作为空值通过。

于 2013-05-16T21:02:43.143 回答