views:

101

answers:

1

Hi

I have a very simple controller and views for displaying and editing user profile data.

The problem is that the form will not post. I cannot see the problem...

Code as follows:

<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>
<div>
    <fieldset>
        <p>
            <label for="Title">
                Title:</label>
            <%= Html.TextBox("Title", Model.Title) %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
        <p>
            <label for="FirstName">
                FirstName:</label>
            <%= Html.TextBox("FirstName", Model.FirstName)%>
            <%= Html.ValidationMessage("FirstName", "*") %>
        </p>
        <p>
            <label for="LastName">
                LastName:</label>
            <%= Html.TextBox("LastName", Model.LastName)%>
            <%= Html.ValidationMessage("LastName", "*") %>
        </p>
    </fieldset>
    <fieldset>
        <legend>Contact with the Encephalitis Society</legend>
        <p>
            <label for="Contactable">
                Allow The Encephalitis Society to contact me (we will not contact you unless this
                is checked):</label>
            <%= Html.CheckBox("Contactable", Model.Contactable)%>
            <%= Html.ValidationMessage("Contactable", "*") %>
        </p>
        <p>
            <label for="SubscribeNewsletter">
                I would like to receive e-newsletters:</label>
            <%= Html.CheckBox("SubscribeNewsletter", Model.SubscribeNewsletter)%>
            <%= Html.ValidationMessage("SubscribeNewsletter", "*") %>
        </p>
        <p>
            <label for="wantMembershipInfoPackage">
                I would like more information about becoming a member of the Encephalitis Society:</label>
            <%= Html.CheckBox("wantMembershipInfoPackage", Model.IsMember)%>
            <%= Html.ValidationMessage("wantMembershipInfoPackage", "*")%>
        </p>
        <p>
            <label for="IsMember">
                I am already a member of the Encephalitis Society:</label>
            <%= Html.CheckBox("IsMember", Model.IsMember)%>
            <%= Html.ValidationMessage("IsMember", "*") %>
        </p>
        <p>
            <label for="wantToBeRegularDonor">
                I would like to make a regular donation to the Encephalitis Society:</label>
            <%= Html.CheckBox("wantToBeRegularDonor", Model.IsMember)%>
            <%= Html.ValidationMessage("wantToBeRegularDonor", "*")%>
        </p>
    </fieldset>
    <hr />
    <%=Html.ActionLink("Cancel (Return to My Page)", "MyPage", "Members", null, new { @class = "LinkButton LeftButton" })%>
    <input class="LinkButton RightButton" type="submit" value="Save" />
</div>
<% } %>

The controller is as follows:

public class ProfileController : Controller
{

    WebProfile p = WebProfile.Current;
    Member member = new Member();

    // GET: Shows details of the Profile
    public ViewResult Show()
    {
        ViewData["CategoryRole"] = member.CategoryRoleUserFriendly;
        return View(p);
    }

    // GET: /Profile/New - displays a template to create the Profile
    public ViewResult New()
    {
        ViewData["SaveButtonText"] = "Next >>";
        return View(p);
    }

    // POST: /Profile/New
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult New(FormCollection formValues)
    {
        try
        {
            WebProfile.GetProfile(member.UserName);
            UpdateModel(p);
            return RedirectToAction("MyPage", "Members");
        }
        catch
        {
            ViewData["SaveButtonText"] = "Next >>";
            return View();
        }
    }

    // GET: /Profile/Edit - displays a template to create the Profile
    public ViewResult Edit()
    {
        ViewData["SaveButtonText"] = "Save >>";
        return View(p);
    }

    // POST: /Profile/Edit - displays a template to create the Profile
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(FormCollection formValues)
    {
        try
        {
            WebProfile.GetProfile(member.UserName);
            UpdateModel(p);
            return RedirectToAction("Show");
        }
        catch
        {
            return View();
        }
    }
}

}

Does anything leap out at you? I just can't see a problem...

Andrew

+1  A: 

Gregoire

I have solved it and it is a such a tiny issue that I will detail it here:

The problem was a missing quote ("), as follows:

    <p class="Note>PLEASE NOTE: All items below are Optional</p>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>
<div>
    <fieldset>
        ...

Can you spot it? Don't worry, it took me a day:

<p class="Note>...

Should have been:

<p class="Note">

The missing quote before the <% using(Html.BeginForm()) %> was enough to scupper the form POST action. There were no errors, no change of code colouring. No visual indication. Nothing.

One to remember!:

When your form won't post, look for malformed html above the Html.BeginForm() line.

Andrew

awrigley