如何从视图中收到动态添加值ASP.NET MVC到控制器? [英] How to receive dynamically added values from view to controller in ASP.NET MVC?

查看:104
本文介绍了如何从视图中收到动态添加值ASP.NET MVC到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习在asp.net的MVC的概念。我使用实体框架和Visual Studio 2013的社区版。我在学习创建演示应用程序。我已经根据链接创建的模型。该型号如下。
下面是课程模式。该课程有部门为外键。一个部门可以有很多的课程。

I am learning some concepts in asp.net mvc. I am using entity framework and visual studio 2013 community edition. I am creating a demo app for learning. I have created model according to this link. The models are as follows. Below is course model. The course has department as foreign key. A department can have many courses.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManyItemsDemo2.Models
{  
    public class Course
    {
        public int CourseID { get; set; }
        public string Title { get; set; }
        public string Credits { get; set; }
        public int DepartmentID { get; set; }

        public virtual Department Department { get; set; }
    }
}

这是部门的模式。该部门的模型很简单。它与课程模式有关。

This is department model. The department model is simple. It is associated with course model.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManyItemsDemo2.Models
{
    public class Department
    {
        public Department()
        {
            this.Cources = new HashSet<Course>();
        }
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public double Budget { get; set; }
        public string Administrator { get; set; }

        public virtual ICollection<Course> Cources { get; set; }
    }
}

有一个上下文类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace ManyItemsDemo2.Models
{
    public class SchoolContext:DbContext
    {
        public SchoolContext() : base("SchoolContext") { }

        public DbSet<Course> Courses { get; set; }
        public DbSet<Department> Departments { get; set; }
    }
}

现在我用脚手架和创建控制器和CRUD功能的看法。我可以创建视图的部门和课程。现在我需要分配而创作部门的多个课程。所以,我创建了一个该视图模型。在这里,一个部门有很多的课程。

Now i have used scafolding and created controllers and views with CRUD functionality. I can create a department and courses in views. Now i need to assign multiple courses while creation of departments. So i created a this view model. Here One department has many courses.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ManyItemsDemo2.Models;

namespace ManyItemsDemo2.ViewModels
{
    public class DeptCourses
    {
        public Department Department { get; set; }
        public IEnumerable<Course> Course { get; set; }
    }

}

随着这我创建了一个新的视角。同时创造部门,可以接受更多的cources。该视图的结果是这样的。

加号按钮有一个脚本,它是从我前面的问题<一href=\"https://stackoverflow.com/questions/36498907/how-to-update-id-and-name-of-html-element-inside-div-element\">here.该脚本使用jQuery和克隆下拉,并增加了回来。

The plus button has a script which is from my earlier question here. The script uses jquery and clones the drop down and adds back.

问题就从这里开始。当我添加多个下拉,让说3,我在控制器中接收空,虽然我收到三个要素,只有一个元素都有价值,其他都是空。看到图像更多的澄清。

The problem starts from here. When i add more than one drop down, let say 3, i am receiving null in controller, though i receives three elements, only one element has value, others are null. See the image for more clarification.

图片澄清。

为什么出现这种情况?
PS:我可能会忽略的实时场景,因为这是学习和清除之一的概念,与实体框架使用MVC一起许多关系演示程序。

Why this happens? PS: I might overlooked real time scenarios as this is demo app for learning and clearing the concepts of one to many relationships with entity framework along with using MVC.

推荐答案

为我工作。就在httppost改变脚本的名称,并添加字符串数组。

This worked for me. Just changed the name in script and added string array in httppost.

这篇关于如何从视图中收到动态添加值ASP.NET MVC到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆