使用C#写入复合SQL表

时间:2013-07-16 17:39:16

标签: c# sql asp.net-mvc-4

我是C#,SQL和ASP.NET MVC世界的新手。我必须写入以下SQL表(不能使用SQL必须使用C#和MVC):

读写

Group
-----------------
id -- identity
name -- varchar

GroupCompanies
----------------
groupId -- fk link to Group.id
companyId -- fk link to Company.id

GroupSoftware
---------------
groupId -- fk link to Group.id
softwareId -- fk link to Software.id
companyId -- fk link to Company.id

有单独的只读表,其中数据来源于制作上述复合表(“GroupCompanies”和“GroupSoftware”):

只读

Companies
-----------------
id -- identity
name -- varchar

Software
-----------------
id -- identity
name -- varchar

在名为“创建”的页面上,我正在处理,我在视图中三个字段

- 名称 //需要写入Group.ID。然后必须与软件和公司配对。

- 公司 //需要通过将所选的Company.ID与上述创建的名称的ID配对来写入GroupCompanies。

- 软件 //需要通过将选定的Company.ID,上面创建的名称和AND与Software.ID配对来写入GroupSoftware。

我需要写一个新组的名称,将其发送到“组”表。这不是什么大问题。我可以成功地使这个工作。

其他两个字段(“公司”和“软件”)是下拉列表。我需要将“Company”值(它们是字符串格式的ID,因此它们需要在post上转换为整数)与“Software”值(也作为字符串的ID)和Post上新创建的组配对。

现在就是这样的事情:如何在发布字段时将值字符串捕获为映射到正确对象的整数,然后一次将它们写入三个表?由于“GroupSoftware”表依赖于“GroupCompanies”和“Groups”,而“GroupCompanies”依赖于“Groups”,我需要按此顺序执行命令。此外,我不知道如何在需要使用C#和ASP.NET MVC的其他表中的ID的表上编写。

如果有人可以提供帮助,我们将不胜感激!

PS:记住,我是新人,我是个白痴,所以请你好! :d

一些代码供参考: MVC VIEW

@{
ViewBag.Title = "Create a Group";
}
<h2>createGroup</h2>
<fieldset>
   <legend>Group</legend>
@Html.Action("Create")
@Html.Action("softwareList")
@Html.Action("companyList")
<input type ="submit" value="Submit" />
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>

需要修改的MVC控制器

[HttpGet]
    public ActionResult softwareList()
    {
        List<SelectListItem> listSelectListItem = new List<SelectListItem>();
        foreach (Software software in db.Software)
        {
            SelectListItem selectListItem = new SelectListItem()
            {
                Text = software.SoftwareName,
                Value = software.SoftwareID.ToString(), //The items in the list have a   string value of SoftwareID.
            };
            listSelectListItem.Add(selectListItem);
        }

        softwareViewModel softwareviewmodel = new softwareViewModel(); //instantiates dummy container
        softwareviewmodel.Applications = listSelectListItem; //the list represent the 'software' in the dummy. A value is a piece of Array.
        return PartialView(softwareviewmodel);//shows the dummy container.
    }

^这是用于软件下拉列表,“groupcompanies”一个是相同的,除了'company'替换'软件'。

MVC模型类

public class softwareViewModel
{
public IEnumerable<SelectListItem> Software { get; set; }
public IEnumerable<string> Selectedsoftware { get; set; }   
}

0 个答案:

没有答案
相关问题