3

我正在制作一个应用程序,其中需要在用户输入手机号码并跳出标签时创建潜在客户,如果输入的手机号码有效(表格上的所有其他详细信息),则会创建潜在客户。但是当我将模型从视图传递到控制器时,我得到的数据为空。

看法

 @{
    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 函数的数据很好,但是当数据到达控制器时它是空的。

4

1 回答 1

1

尝试contentType从您的 ajax 调用中删除:

DoServerRequest: function (url, data, funSuccess) {
    $.post({
        type: "POST",
        url: url,
        data: data,
        async: false,
        dataType: "json",
        success: funSuccess,
        error: function () {
            alert("Error in getting list");
        }
    });
}

服务器不期望 JSON 内容,而是 application/x-www-form-urlencoded,这是默认值。

这意味着您还应该使用普通的 Javascript 对象或字符串作为数据:

if (mobileNumber.match(number) && mobileNumber.length == 10) {
    Events.DoServerRequest('/MAXVSM/Quote/test', $("#frmEmailQuote").serialize(), GetLeadId);
}
于 2013-04-08T08:35:05.073 回答