如何讓我的控制器操作接受電子郵件作爲參數。將電子郵件ID作爲操作參數接受MVC3中的控制器
這些路線我
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);
}
。但是如果我試圖輸入像'[email protected]'這樣的值,我會得到'404 not found'錯誤。
爲什麼不以JSON格式發送? –
試過JSon.Stringfy(id)....但它不工作。 – Naresh