使用ajax调用将模型从视图传递到控制器?

时间:2013-04-08 05:05:38

标签: asp.net-mvc jquery razor

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

查看

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

1 个答案:

答案 0 :(得分:1)

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

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);
}