asp.net-mvc - 使用ajax调用将模型从 View 传递到 Controller ?

标签 asp.net-mvc jquery razor

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

查看

 @{
    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 Function

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

数据从 View 到ajax函数到达正常,但是当数据到达 Controller 时它为空。

最佳答案

尝试从 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);
}

关于asp.net-mvc - 使用ajax调用将模型从 View 传递到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15871652/

相关文章:

c# - 在 asp.net mvc 中以编程方式中止 OutputCache 持续时间

asp.net-mvc - asp.net mvc - 检查调用了什么 Controller 和方法?

asp.net-mvc - 使用 WebGrid 或不使用 WebGrid...答案是什么?

jquery - 将img包装在<li>标记中

c# - 使用 MVC/Razor 传递 C# 变量以与 Dygraph 一起使用

javascript - 在传递 C# 参数的 document.Load() 上调用 javascript

c# - 如何在服务器端使用 C# 查询和提取 html 字符串中的特定标记?

jquery - 如何使用 JQuery 启用/禁用基于单选按钮的文本框

Jquery返回元素html而不是内容

c# - Blazor 组件需要修改父组件的值