我编写了一些代码,假设使用Ajax和@ Html.BeginForm将值传递给控制器。
但似乎我的Ajax正在推翻我的@ Html.BeginForm提交,因此模型属性没有被填充。
查看
<script>
$(document)
.on("click",
".submit",
function(e) {
var val = $(".Date").val();
$.ajax({
url: '/EventSubmission/Submit',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({ "dateTime": val })
});
});
</script>
@using (Html.BeginForm("Submit", "EventSubmission"))
{
<h3>Your contact details</h3>
<h5>Name</h5>
@Html.TextBoxFor(o => o.Arranger.Contact_Name);
<h5>Email address</h5>
@Html.TextBoxFor(o => o.Arranger.Contact_Phone);
<h5>Phone number</h5>
@Html.TextBoxFor(o => o.Arranger.Contact_Email);
<h5> Date and time of event:</h5>
<input type="date" id="Date" class="Date"/> <input type="time" id="Time" />
<input type="submit" value="submit" class="submit"/>
}
控制器
[HttpPost]
public ActionResult Submit(Event ev, DateTime dateTime)
{
ev.DateOfEvent = dateTime;
string text = "<html> <head> </head>" +
" <body style= \" font-size:14px; font-family: Arial\">" +
"<b>Contact Information</b> <br>" +
"Name: " + ev.Arranger.Contact_Name + "<br>" +
"Email: " + ev.Arranger.Contact_Email + "<br>" +
"Phone: " + ev.Arranger.Contact_Phone + "<br>" +
"<b>Company Details</b> <br>" +
"Name: " + ev.Company.CompanyName + "<br>" +
"Size: " + ev.Company.CompanySize + "<br>" +
"Website: " + ev.Company.CompanyWebsite + "<br>" +
"<b>Event Type</b> <br>" +
ev.Type + "<br>" +
"<b>Event Details</b> <br>" +
"Name: " + ev.Name + "<br>" +
"Address: " + ev.Location.Address + "<br>" +
"Postcode: " + ev.Location.Postcode + "<br>" +
"Local MP Attending?: " + ev.LocalMpName + "<br>" +
"Open to Media?: " + ev.EventMedia + "<br>" +
"Open to Public: " + ev.OpenToPublic + "<br>" +
"Date and Time: " + ev.DateOfEvent + "<br>" +
"Description: " + ev.Description + "<br>" +
"Estemated Antendance: " + ev.Attendees + "<br>" +
"Show on Evnt Map: " + ev.ShowOnWebsite +
"</body></html>";
SendEmail("rubidgetest1@gmail.com", text);
Event tempForm = new Event();
return View(tempForm);
}
这当前不起作用,因为没有填充模型属性,唯一填充的是ev.DateOfEvent,我猜这是因为Ajax是唯一发布到控制器的东西。
基本上我希望Ajax和@ Html.BeginForm()发布到控制器