开发者

Master Detail ASP.Net MVC

I'm new to ASP.Net MVC and have a question regarding a master detail input form.

My database has a Child Table with foreign key relationships 开发者_JAVA技巧to the Physician, Immunization and Parent Table. I've used Linq to SQL to create my model.

I've created the controller and view for Child. The user will come to the form and submit everything all at once - a Child, their Physician info, one or many Parents and one or many Vaccinations.

I'm not sure how to approach this. Do I need a controller for Vaccinations, Parents etc?

My pre MVC app simply grabbed everything from the web form and populated all the


I typically have one controller that supports several views (add, edit, delete, etc.). I read from my database into a model that has fields for each piece of data that you want in the view. You then pass the model to the view so it can render it.

After the form is submitted, you'll get a model back from it as a parameter to the controller. You then update the database as needed.

Here's some code. I didn't try compiling it so your mileage may vary.

    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);
    }

The view (abbreviated):

<%@ 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>

        .
        .
        .

    <%} %>


You only need the master controllers. The trick for editing list of thinks (the child objects) is explained in this haacked post

Basically you need to follow this conventions and MVC will fill an array of child object in the post method:

<% 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) %> 

<% } %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜