无法获得[HttpPost]回复

时间:2018-01-14 12:06:07

标签: c# asp.net-mvc

刚刚开始学习ASP MVC,我试图在你提供你的名字和电子邮件的地方制作这个表格......当你点击提交表格时,它应该导致"谢谢"您看到感谢信息和您的姓名的页面。 但点击"提交"按钮没有发生在我身上。我想知道问题是什么。 这是所有行动方法的Homecontroller

using System;
using Microsoft.AspNetCore.Mvc;
using PartyInvites.Models;

  namespace PartyInvites.Controllers
{
  public class HomeController : Controller
{

    public ViewResult Index()
    {
        int hours = DateTime.Now.Hour;
        ViewBag.Greeting = hours < 12 ? "Good Morning" : "Good Evening";
        return View("MyView") ;
    }


    [HttpGet]
    public ViewResult RsvpForm()
    {
        return View();
    }

    [HttpPost]
    public ViewResult RsvpForm(GuestResponse guestResponse)
    {
        Repository.AddResponse(guestResponse);

        return View("Thanks",guestResponse);
    }

}

}

这是&#34; MyView&#34; 页面,这是默认页面

@{
Layout = null;
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewBag.Greeting World (from the view)
        <p>
            We are going to have an exciting party.<br />
            (to do:sell it better.Add pictures or something.)
        </p>
        <a asp-action="RsvpForm"> Ravp Now</a>

    </div>
</body>
</html>
}

这是表单页,点击 Ravp Now

后,您可以访问该页面
*@
 @model PartyInvites.Models.GuestResponse

 @{ Layout = null;
  }
 <!DOCTYPE html>
  <html>
 <head>
<meta name="viewpot" content="width=device-width"/>
<title>RsvpForm</title>
 </head>
 <body>
  <form asp-action="RsvpForm" method="post"></form>
 <p>
<label asp-for="Name"> Your Name: </label>
<input asp-for="Name"/>
</p>
<p>
<label asp-for="Email"> Your Email:</label>
<input asp-for="Email"/>
</p>
<p>
<label asp-for="Phone"> Your Phone:</label>
<input asp-for="Phone"/>
</p>
<p>
<label> Will You Attend?</label>
<select asp-for="WillAttend">
    <option value=""> Choose an option </option>
    <option value="true"> Yes, I'll be There. </option>
    <option value="false"> No, i can't come. </option>
</select>
</p>
<button type="submit"> Submit Rsvp</button>
</body>
</html>

现在当我点击提交Rsvp 时,问题出在这里,没有任何事情发生。它不会进入&#34;谢谢&#34;页。我检查了几次所有代码,但找不到任何东西。 可能有什么不对? 这些是&#34;感谢&#34; 查看页面和&#34;存储库&#34; 类模型

@model GuestResponse
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head >
<meta name="viewport" content="width=device-width"/>
<title>Thanks</title>
</head>
<body>
<p>
<h1> Thank you! @Model.Name!</h1>
@if (Model.WillAttend == true)
{
    @:Its great that you're coming.The drinks are already in the Fridge!
}
else
{
@:sorry to here that you can't make it , but thanks for letting us know.
}
</p>
<p> click<a asp-action="ListResponses"> here </a> to see who is coming.</p>
</body>
</html>
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Threading.Tasks;

  namespace PartyInvites.Models
 {
 public static class Repository
 {
    private static List<GuestResponse> responses=new List<GuestResponse>();

    public static IEnumerable<GuestResponse> Responses
    {
        get { return responses; }
    }

    public static void AddResponse(GuestResponse response)
    {
        responses.Add(response);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为您的问题出在您的cshtml文件中。

表格的所有内容必须在此表格声明范围内。我现在没有可用于检查它的C#项目,但我认为“固定”代码将是这样的:

*@
 @model PartyInvites.Models.GuestResponse

 @{ Layout = null;
  }
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewpot" content="width=device-width"/>
        <title>RsvpForm</title>
    </head>
    <body>
        <form asp-action="RsvpForm" method="post">
            <p>
                <label asp-for="Name"> Your Name: </label>
                <input asp-for="Name"/>
            </p>
            <p>
                <label asp-for="Email"> Your Email:</label>
                <input asp-for="Email"/>
            </p>
            <p>
                <label asp-for="Phone"> Your Phone:</label>
                <input asp-for="Phone"/>
            </p>
            <p>
                <label> Will You Attend?</label>
                <select asp-for="WillAttend">
                    <option value=""> Choose an option </option>
                    <option value="true"> Yes, I'll be There. </option>
                    <option value="false"> No, i can't come. </option>
                </select>
            </p>
            <button type="submit"> Submit Rsvp</button>
        </form>
    </body>
</html>