Master Detail ASP.Net MVC

时间:2010-11-17 20:43:35

标签: asp.net-mvc-2

我是ASP.Net MVC的新手,对主细节输入表格有疑问。

我的数据库有一个子表,其中包含与Physician,Immunization和Parent Table的外键关系。我使用Linq to SQL来创建我的模型。

我为Child创建了控制器和视图。用户将来到表格并一次性提交所有内容 - 儿童,他们的医生信息,一个或多个父母以及一个或多个接种疫苗。

我不确定如何处理这个问题。我是否需要接种疫苗,父母等控制器?

我的预MVC应用程序只是从网络表单中抓取所有内容并填充所有

2 个答案:

答案 0 :(得分:0)

我通常有一个支持多个视图的控制器(添加,编辑,删除等)。我从我的数据库中读到一个模型,该模型包含视图中所需的每个数据的字段。然后,您将模型传递给视图,以便它可以呈现它。

提交表单后,您将从中获取一个模型作为控制器的参数。然后根据需要更新数据库。

这是一些代码。我没有尝试编译它,所以你的里程可能会有所不同。

    public class ParentModel
    {
        // Parent fields
        public string FirstName { get; set; }
        public string LastName { get; set; }

        IList<ChildModel> Children { get; set; }
    }

    public class ChildModel
    {
        // Child fields
        public int Age { get; set; }
    }


    public ActionResult MyAction()
    {
        // Grab a model with the children property filled out
        ParentModel myModel = GetFromDatabase();

        return View("MyView", myModel);
    }

观点(缩写):

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<ParentModel>" %>

.
.
.


<% using (Html.BeginForm("MyAction", "MyController", FormMethod.Post)) %> 
<% {%>
        <%= Html.ValidationSummary(true) %> 

        <table>
            <tr valign="top">
                <td>
                    <%:Html.LabelFor(a => a.FirstName)%>:
                </td>
                <td>
                    <%:Html.EditorFor(a => a.FirstName)%>
                    <%:Html.ValidationMessageFor(a => a.FirstName)%>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <%:Html.LabelFor(a => a.LastName)%>:
                </td>
                <td>
                    <%:Html.EditorFor(a => a.LastName)%>
                    <%:Html.ValidationMessageFor(a => a.LastName)%>
                </td>
            </tr>
        </table>

        .
        .
        .

    <%} %>

答案 1 :(得分:0)

您只需要主控制器。在this haacked post

中解释了编辑think(子对象)列表的技巧

基本上你需要遵循这个约定,MVC将在post方法中填充一个子对象数组:

<% for (int i = 0; i < 3; i++) { %>

  <%: Html.TextBoxFor(m => m[i].Title) %>
  <%: Html.TextBoxFor(m => m[i].Author) %>
  <%: Html.TextBoxFor(m => m[i].DatePublished) %> 

<% } %>