gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:35:12 25 4
gpt4 key购买 nike

我正在制作一个应用程序,其中需要在用户输入手机号码并退出时创建潜在客户,如果输入的手机号码有效(以及表单上的所有其他详细信息),则会创建潜在客户。但是当我将模型从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com