我正在制作一个应用程序,其中需要在用户输入手机号码并跳出标签时创建潜在客户,如果输入的手机号码有效(表格上的所有其他详细信息),则会创建潜在客户。但是当我将模型从视图传递到控制器时,我得到的数据为空。
看法
@{
ViewBag.Title = "EmailQuote";
Layout = "~/Views/Shared/Template.cshtml";
}
@model Max.VirtualSalesManager.UI.ViewModels.LeadViewModel
@{
var data = Html.Raw(Json.Encode(Model));
}
<script src="@Url.Content("~/Scripts/Common.js")" type="text/javascript"></script>
<h2>
EmailQuote</h2>
@using Acidaes.VirtualSalesManager.UI.Helpers
@using (Html.BeginForm("GenerateEQuote", "Quote", FormMethod.Post, new{id="frmEmailQuote" }))
{
@Html.MandatoryLabel("Name", false, new { style = "width: 50px; display: inline" }) @Html.FullName()
@Html.MandatoryLabel("City", false, new { style = "width: 50px; display: inline" }) @Html.DropDownListFor(m => m.CommunicationAddress.City, new SelectList(ViewBag.CityList, "Key", "Value"), "select", new { id = "ddlCity" })
@Html.MandatoryLabel("Mobile No.", false, new { style = "width: 100px; display: inline" }) @Html.TextBoxFor(m => m.MobileNumber, new { style = "width: 100px", id = "txtmobilenumber", onBlur = "createLead('"+@data+"')" })
@Html.MandatoryLabel("Email ID", false, new { style = "width: 80px; display: inline" }) @Html.TextBoxFor(m => m.EmailId, new { style = "width: 100px" })
@Html.HiddenFor(m => m.LeadId)
<input type="submit" id="btnSubmit" />
}
JS
function createLead(data) {
var number = /^[0-9]+$/;
var mobileNumber = document.getElementById('txtmobilenumber').value;
if (mobileNumber.match(number) && mobileNumber.length == 10) {
var jsondata = JSON.stringify(data);
var Parameters = "{'data':'" + ("#frmEmailQuote").serialize() + "'}";
Events.DoServerRequest('/MAXVSM/Quote/test', Parameters, GetLeadId);
}
}
DoServerRequest 函数
DoServerRequest: function (url, data, funSuccess) {
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: funSuccess,
error: function () {
alert("Error in getting list");
}
});
Controller
[HttpPost]
[ActionName("test")]
public JsonResult test(LeadViewModel data)
{
LeadViewModel t = new LeadViewModel();
t.MobileNumber = data.MobileNumber;
return Json(t);
}
从视图到 ajax 函数的数据很好,但是当数据到达控制器时它是空的。