views:

51

answers:

1

Greeting Everyone,

I'm having a little trouble with a partial view I'm using as an edit form. Aside from the hidden elements in the form, the model is null when passed to my EditContact post controller.

I should add, I realize there's a more seamless way to do what I'm doing using AJAX, but I think if I can solve the clunky way first it will help me understand the framcework a little better. Here's my EditContact partial view:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WebUI.DataAccess.CONTACT>" %>
<div id="contactPartial-<%= ViewData.Model.id %>">
    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm("EditContact", "Investigator"))
       { %>
    <%= Html.HiddenFor(Model => Model.id) %>
    <%= Html.HiddenFor(Model => Model.investId) %>
    <table id="EditContact-<%= ViewData.Model.id %>" width="100%">
        <tr>
            <th colspan="4">
                <b>New Contact</b>
            </th>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.type)%></b><br />
                <%= Html.EditorFor(Model => Model.type, null, "contactType-" + ViewData.Model.id)%><br />
                <%= Html.ValidationMessageFor(Model => Model.type) %>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.salutation)%></b><br />
                <%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.firstName)%></b><br />
                <%= Html.EditorFor(Model => Model.firstName, null, "contactFirstName-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.middleName)%></b><br />
                <%= Html.EditorFor(Model => Model.middleName, null, "contactMiddleName-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.lastName)%></b><br />
                <%= Html.EditorFor(Model => Model.lastName, null, "contactLastName-"+ViewData.Model.id)%><br />
                <%= Html.ValidationMessageFor(Model => Model.lastName)%>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.title)%></b><br />
                <%= Html.EditorFor(Model => Model.title, null, "contactTitle-"+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.phone)%></b><br />
                <%= Html.EditorFor(Model => Model.phone, null, "contactPhone="+ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.altPhone)%></b><br />
                <%= Html.EditorFor(Model => Model.altPhone, null, "contactAltPhone-" + ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.fax)%></b><br />
                <%= Html.EditorFor(Model => Model.fax, null, "contactFax-" + ViewData.Model.id)%>
            </td>
        </tr>
        <tr>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.email)%></b><br />
                <%= Html.EditorFor(Model => Model.email, null, "contactEmail-" + ViewData.Model.id)%>
            </td>
            <td>
                <b>
                    <%= Html.LabelFor(Model => Model.comment)%></b><br />
                <%= Html.EditorFor(Model => Model.comment, null, "contactComment-" + ViewData.Model.id)%>
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Save" />
            </td>
            <td>
                <button id="<%= ViewData.Model.id %>" class="contactEditHide">
                    Cancel</button>
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
    </table>
    <% } %>
</div>

When the Edit contact post controller is passed the model only id and investId have values; everything else is null. Here's how I'm rendering the partial view in my Details view:

   <table width="100%">
    <tr>
        <th colspan="7">
            <b>Contacts</b>
        </th>
        <th id="contactButton" class="button">
            <button id="showEditContact-0" type="button" class="contactEditShow">New Contact</button>
        </th>
    </tr>
    <tr>
        <th>
            Type
        </th>
        <th>
            Name
        </th>
        <th colspan="2">
            Title
        </th>
        <th>
            Prim. & Alt. Phone
        </th>
        <th>
            Fax
        </th>
        <th colspan="2">
            Comment
        </th>
    </tr>
    <% for (int i = 0; i < Model.CONTACTs.Count; i++)
       { %>           
    <tr id="contactRow-<%= Model.CONTACTs[i].id %>" class="contactRow">
        <td>
            <%= Html.Encode(Model.CONTACTs[i].type)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].salutation)%>
            <%= Html.Encode(Model.CONTACTs[i].firstName)%>
            <%= Html.Encode(Model.CONTACTs[i].middleName)%>
            <%= Html.Encode(Model.CONTACTs[i].lastName)%>
            <br />
            <a href="mailto:<%= Html.AttributeEncode(Model.CONTACTs[i].email) %>">
                <%= Html.Encode(Model.CONTACTs[i].email)%></a>
        </td>
        <td colspan="2">
            <%= Html.Encode(Model.CONTACTs[i].title)%>
        </td>
        <td>
            Prim:<%= Html.Encode(Model.CONTACTs[i].phone)%><br />
            Alt:<%= Html.Encode(Model.CONTACTs[i].altPhone)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].fax)%>
        </td>
        <td>
            <%= Html.Encode(Model.CONTACTs[i].comment)%>
        </td>
        <td>
            <button id="<%= Model.CONTACTs[i].id %>" type="button" class="contactEditShow">Edit Contact</button>
        </td>
    </tr>
    <tr>
        <td colspan="8">
            <% Html.RenderPartial("EditContact", Model.CONTACTs[i]); %>
        </td>
    </tr>
    <% } %>
</table>

My details view is strongly tyed to an INVESTIGATOR model:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.DataAccess.INVESTIGATOR>" %>

As you can see above, my partial view is strongly typed to a CONTACTS model.

I ran into a similar problem with Model binding when, in a view, I was trying to allow for the editing all layers of an address at one time and fixed it by indexing the model, similar to the Details view above. It seems like I need to do the same thing in my partial view to ensure Model binding, but I'm not sure how to achieve this (having syntax issues).

Of course, I could be off here an need a new solution in general! Let me know if you need more detail! Thanks!

+1  A: 

Why are you using

<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>

If you use it simply like this

<%= Html.EditorFor(Model => Model.salutation) %>

It should be working.

In order to map your model, your control need to have the same id as there name, and the third parameter you're using changes your control id.

moi_meme
Well... interesting! That worked. I'll mark this as answered. I was renaming the ids so that they would be unique. For example, I need to be able to clear the contact partial when a new contact is being entered but the user cancels out. Contact has a type and there's another partial in the view for address which has a type also. Both ids have the same name in the html. I'll play with this a little more to see if I can fugure it out. Thanks again.
mr_plumley