【问题标题】:How to insert in your base using parameters from different Views in ASP MVC如何在 ASP MVC 中使用来自不同视图的参数插入您的基础
【发布时间】:2013-05-11 17:08:14
【问题描述】:

我正在使用 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

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

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

【问题讨论】:

  • 您在 MVC 3 应用程序中使用贬低的 .aspx 和 .ascx 代替 razor 代码是否有原因?
  • 是的,因为我使用的模板不仅仅适用于 aspx 或 ascx
  • 执行是指提交表单吗?如果是,我猜您会收到该错误,因为 ModelState 无效,或者 model.YourGammeModel 不是您的视图所期望的。
  • 是的执行意味着提交表单!我不知道,,,我会尝试一个断点并给你结果
  • @anouar - 不,因为我无法理解你的问题。你的整个问题对我来说毫无意义。

标签: c# asp.net-mvc-3 visual-studio-2010


【解决方案1】:

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

return View(model.YourGammeModel);

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

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

编辑:再看一遍,我怀疑ModelState 无效,因为您在IDv 上具有[Key] 属性,并且它以null 的形式出现。

【讨论】:

  • 是的,你的想法是对的,,,我删除了 ModelState 的测试,,,但是现在 ID_Gamme 仍然为空,,,这个错误出现:无法将值 NULL 插入到列 'ID_Gamme' 表中'Flux.dbo.Gamme'。此列不接受 NULL 值。插入失败。声明已终止。
  • 是表的主键吗?如果您手动设置它,您可以在声明 model.YourGammeModel = new Gamme(); 时将其更改为 new Gamme { ID_Gamme = whatever },或者您可以更改数据库设置以自动生成 ID,具体取决于 ID_Gamme 的数据类型
  • 不,我不能自动生成 ID_Gamme,因为它是从 Profil_Ga 表中加载的。所以要恢复:我有:ID_Gamme(从Profil_ga表加载)+其余参数(从Poste表加载)==>所有这些参数都必须记录在Gamme表中
  • 那么在你有public ActionResult Index(Profile_Ga profile_ga, Poste poste) 的地方,profile_ga 里面有没有 ID?
  • 它超越了一个错误,成为更大的设计问题。我要离线过夜,稍后再试。
猜你喜欢
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多