0

如何讓我的控制器操作接受電子郵件作爲參數。將電子郵件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'錯誤。

+0

爲什麼不以JSON格式發送? –

+0

試過JSon.Stringfy(id)....但它不工作。 – Naresh

回答

1

怎麼樣發送這樣的:

$.ajax({ 
       type: 'POST', 
       url: '/Info/SubscribeToNewsLetter', 
       data: {"email" : Id }, 
       contentType: "application/json; charset=utf-8", 
       traditional: true, 
       success: subscribed, 
       error: errorInSubscribing 
      }); 

編輯:

嘗試它周圍雙引號內:

$.ajax({ 
       type: 'POST', 
       url: '/Info/SubscribeToNewsLetter', 
       data: '{"email" : "' + Id + '"}', 
       contentType: "application/json; charset=utf-8", 
       traditional: true, 
       success: subscribed, 
       error: errorInSubscribing 
      }); 
+0

獲取內部服務器錯誤。無效的JSON基元:電子郵件。 \ r \ n \ r \ n \ r \ n \ r \ n描述:執行當前Web請求期間發生未處理的異常。 \ r \ n \ r \ n \ r \ n \ r \ n異常詳細信息:System.ArgumentException:無效的JSON原語:電子郵件。請檢查堆棧跟蹤以獲取有關錯誤的更多信息以及源代碼的來源。 – Naresh

+0

@Naresh可以嗎? –

+0

將電子郵件更改爲其他內容。可能是特殊關鍵字。 –

相關問題