如何使我的控制器操作以接受电子邮件作为参数。
这些是我的路线
routes.MapRoute(
name: "infoNewsletterSignup",
url: "info/SubscribeToNewsLetter/{email}",
defaults: new { controller = "Info", action = "SubscribeToNewsLetter", email =UrlParameter.Optional});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Info", action = "Index", id = UrlParameter.Optional }
);
这是我的控制器。
[HttpPost]
public string SubscribeToNewsLetter(string email)
{
return "Thanks!\n\nYou have successfully subscribed for our newsletter.";
}
这是我的观点
<td>
<input type="text" class="text-bx-foot" value="" id='newsLetterEmail' name='newsLetterEmail'></td>
<td>
<input name="Go" type="button" class="go-btn" value="Go" id="btnSubscribeToNewsLetters"></td>
$(document).ready(function () {
$("#btnSubscribeToNewsLetters").click(subscribeToNewsLetters);
});
function subscribeToNewsLetters() {
var objInputEmail = $("#newsLetterEmail");
if (objInputEmail != null) {
var id = objInputEmail.val();
if (id != null) {
$.ajax({
type: 'POST',
url: '/Info/SubscribeToNewsLetter/' + id,
//data: $('#form').serialize(),
contentType: "application/json; charset=utf-8",
traditional: true,
success: subscribed,
error: errorInSubscribing
});
}
}
}
function subscribed(data, textStatus, xhr) {
alert(data);
}
function errorInSubscribing(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
如果我像普通文本一样输入值,则将调用该操作。但是,如果我尝试输入“myname@test.co.uk”之类的值,则会收到“404 not found”错误。