2010-02-11 56 views
1

我有一個非常簡單的控制器和視圖來顯示和編輯用戶配置文件數據。ASP.NET MVC表單不會發布

問題是表單不會發布。我看不到的問題...

代碼如下:

<%= 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> 
<% } %> 

控制器如下:

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

}

做任何事情你撲過來?

+0

如果你把你的動作名稱放在Html.BeginForm()中會發生什麼? – Gregoire 2010-02-11 14:06:00

回答

4

我已經解決了它,它是這樣一個微小的問題,我將詳細介紹在這裏:

的問題是缺少引號(「),如下所示:

<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> 
     ... 

你能找出?它別擔心,我花了一天:

<p class="Note>... 

本來應該是:

<p class="Note"> 

使用(Html.BeginForm())%>之前<%缺少的報價足以破壞表單POST操作。沒有錯誤,沒有改變代碼着色。沒有視覺指示。沒有。

一記!:

當你的形式將不會發布,查找Html.BeginForm()線以上畸形的HTML。