I have a simple post from form .net core and i want to post to that end point with separate front end html, but i have some problem why the object is always null value form html using post Ajax method
I hope anyone can help me thanks.
public class MessageWeb
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string subject { get; set; }
public string message { get; set; }
public DateTime created_date { get; set; }
public bool is_read { get; set; }
}
logger.LogInformation("inside message web endpoint");
var exist = await db.MessageWebs.Where(m => m.email.ToLower() == model.email.ToLower()).FirstOrDefaultAsync();
if (exist !=null)
{
return Json(new { Data = "0" });
}
MessageWeb ms = new MessageWeb()
{
name = model.name,
email = model.email,
phone = model.phone,
subject = model.subject,
message = model.message,
created_date = DateTime.Now,
is_read = false
};
db.MessageWebs.Add(ms);
await db.SaveChangesAsync();
await db.SaveChangesAsync();
return Json(new {Data="1"});
<div class="bg-white p-5 contact-form">
<div class="form-group">
<input type="text" id="name" name="name" class="form-control" placeholder="Your Name" required>
</div>
<div class="form-group">
<input type="text" id="email" name="email" class="form-control" placeholder="Your Email" required>
</div>
<div class="form-group">
<input type="text" id="phone" name="phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<input type="text" id="subject" name="subject" class="form-control" placeholder="Subject" required>
</div>
<div class="form-group">
<textarea name="message" id="message" cols="30" rows="7" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group">
<input type="submit" id ="submit" value="Send Message" class="btn btn-primary py-3 px-5">
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#submit").on("click", function() {
//alert();
var name = $('#name').val();
var email = $("#email").val();
var phone = $("#phone").val();
var subject = $("#subject").val();
var message = $("#message").val();
var model =
{
name: name,
email: email,
phone: phone,
subject:subject,
message:message
};
$.ajax({
url: "URL",
type: "POST",
data: model,
dataType: 'json',
contentType: false,
processData: false,
enctype:'multipart/form-data',
success: function (data) {
if(data == "1"){
alert("success sent message");
}
if(data == "0"){
alert("you alread sent message before");
}
},
error: function (jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
})
});
</script>