我查看了哪个名称是感谢,我想在点击提交后显示此视图,但遇到错误如下:无法找到网页 这是我的代码:
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
return View ("MyView");
}
[HttpGet]
public ViewResult RsvpForm()
{
return View("RsvpForm");
}
[HttpPost]
public ViewResult RsvpForm(GuestRespons GuestRespons)
{
Repository.AddResponse(GuestRespons);
return View("Thanks", GuestRespons);
}
}
这是我的观点:
@{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)
{
@:it's great that you are comming.we will see you soon.
}
else
{
@:Sorry to hear that, but Thanks for letting us to know.
}
</p>
<p >
click <a asp-action="ListResponses">here</a> to see who is coming.
</p>
</body>
</html>
这个是我的按钮,应该显示感谢视图:
@model FuturGoals_partyinvites_.Models.GuestRespons
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
</head>
<body>
<form asp-action="RcvpForm" 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 come?</label>
<select asp="WillAttend">
<option value="">Choose an option</option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
</p>
<button type="submit">Submit RSVP</button>
</form>
</body>
</html>
好吧,我不知道如何在mvc中获得异常,我认为问题应该在这一行:
<button type="submit">Submit RSVP</button>
提前致谢。
答案 0 :(得分:0)
您的表单操作属性值应该是您应用中的有效网址。如果您使用的是表单标记帮助程序,则需要确保将要提交的有效HttpPost操作方法名称作为asp-action
属性值传递。
在您的情况下,您的http发布操作方法名称为RsvpForm
,但在您的表单中,您有RcvpForm
这不是有效的操作方法。因此,修复表单标记帮助程序以使用正确的操作方法,现在它应该可以正常工作。
<form asp-action="RsvpForm" method="post">
<!-- Your existing form elements goes here-->
</form>
答案 1 :(得分:0)
做类似的事情:
<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 come?</label>
<select asp="WillAttend">
<option value="">Choose an option</option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
</p>
<button type="Button" id="submitme" data-url="@Url.Action("RsvpForm","Home")">Submit RSVP</button>
Ajax形式如:
$("#submitme").on("click", function () {
var url = $(this).data('url');
$.ajax({
type: 'post',//this can be change to get or post
url: url,
success: function (response) {
if (response != null && response.success) {
Alert("Success");
window.location.href = "/Home/ActionNameHere"; //You can redirect any action name here....
} else {
// DoSomethingElse()
Alert("something wrong");
}
}
});
});