Guest User

Guest User

  • Tech Writer
  • 611
  • 130.6k

Null Value face error in mvc.

Sep 12 2020 5:08 AM
It's me email send code in mvc but i have face issue .I have enter toEmail in textbox and body but here null value face issue) 
public IActionResult SendEmail(string toEmail, string ToName, string from, string FromName, string Subject, string body)
{
try
{
string admin_email = "****";
string admin_emailpwd = "*****";
MailMessage mail = new MailMessage();
mail.To.Add(toEmail);
mail.From = new MailAddress(admin_email);
mail.Subject = Subject;
mail.Body = body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.smtp2go.com";
smtp.Port = 2525;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential(admin_email, admin_emailpwd);
smtp.EnableSsl = true;
smtp.Send(mail);
 
it's me html and jquery code .Jquery code get textbox value jquery code working fine.
 
<div class="modal fade" id="add-reply" tabindex="-1" role="dialog" aria-labelledby="add-reply">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title has-icon ms-icon-round">Answer to the Query</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body">
<form action="/action_page.php">
<div class="ms-form-group has-icon">
@*<textarea rows="1" class="form-control" placeholder="Email"></textarea>*@
<textarea rows="1" tex class="form-control" placeholder="Email" id="txtTo"></textarea>
</div>
<div class="ms-form-group has-icon">
<textarea rows="4" class="form-control" placeholder="Enter your text here" id="txtBody"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-info shadow-none" id="btnSend">Submit</button>
</div>
</div>
</div>
</div>
 
$("#btnSend").click(function () {
debugger;
console.log("click");
var toEmail = $.trim($("#txtTo").val());
//var subject = $.trim($("[id*=txtSubject]").val());
var body = $.trim($("[id*=txtBody]").val());
$.ajax({
type: "POST",
url: "SendEmail",
data: "{ toEmail: '" + toEmail + "', body: '" + body + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.responseText);
},
failure: function (r) {
alert(r.responseText);
}
});
return false;
});
});
Please check this 

Answers (7)

2
Jignesh Kumar

Jignesh Kumar

  • 33
  • 37.8k
  • 2.8m
Sep 12 2020 11:12 AM
Hi Sourabh,
 
Please make your parameter as object and pass in json string 
  1. var model = { toEmail: $('#txtTo').Val(),   
  2.              body: $('#txtbody').Val()  
  3.             };  
  4.   
  5. $.ajax({  
  6.     type: "POST",  
  7.     data: JSON.stringify(model),  
  8.     url: url,  
  9.     contentType: "application/json"  
  10. }).sucess(function (res) {  
  11.    //  
  12. });  
 in Controller take model  as [FromBody] 
  1. [HttpPost]  
  2. public IActionResult SendEmail([FromBody] ReportManangementViewModel model)  
  3. {  
  4.     //  
  5. }  
 Please refer,
https://stackoverflow.com/questions/33947882/pass-model-to-controller-using-jquery-ajax 
 
Accepted Answer
1
Guest User

Guest User

  • Tech Writer
  • 611
  • 130.6k
Sep 12 2020 8:32 AM

sanaullah sanaullah not working .

1
sanaullah sanaullah

sanaullah sanaullah

  • 0
  • 61
  • 1
Sep 12 2020 6:59 AM
Please change type: "POST",   into type: "Get", in ajax call then you will be able to get value in mvc action parameter
1
Guest User

Guest User

  • Tech Writer
  • 611
  • 130.6k
Sep 12 2020 6:31 AM
Are you able to call method? If not please share updated code. -yes sir.
 
1
Jignesh Kumar

Jignesh Kumar

  • 33
  • 37.8k
  • 2.8m
Sep 12 2020 6:26 AM
Are you able to call method? If not please share updated code.
1
Guest User

Guest User

  • Tech Writer
  • 611
  • 130.6k
Sep 12 2020 6:16 AM

Jignesh Kumar i have changes this.But not working

1
Jignesh Kumar

Jignesh Kumar

  • 33
  • 37.8k
  • 2.8m
Sep 12 2020 5:13 AM
Hi,
 
I think you want be able to call SendEmail() method becasue you have not passed all parameter whiel doing ajax call. Your method SendEmail has 6 parameter which you need to pass while calling from Ajax. check you Action method and pass all parameters in ajax call then it will work.
 
public IActionResult SendEmail(string toEmail, string ToName, string from, string FromName, string Subject, string body)