ajax 传递model,MVC3使用JQuery Ajax将ViewModel传递给控制器​​方法

博主遇到一个问题,在尝试使用JQueryAjax将表单数据提交到ASP.NET MVC控制器的方法时,ViewModel参数始终为null。ViewModel包含了多个属性,包括一个SelectList。在JavaScript中,他们正确地获取了表单字段的值并构造了一个对象。然而,当调用Ajax方法并将数据发送到控制器的HttpPost方法时,ViewModel没有被正确绑定。博主寻求解决ViewModel映射和数据绑定问题的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试使用JQuery Ajax将表单的数据传递到我的控制器方法,但是由于在Controller

Side上使用调试器时ViewModel为null,因此我不确定如何执行此操作。

我的ViewModel是:

public class PremisesViewModel

{

public string createPremisesErrorMessage { get; set; }

public string updatePremisesErrorMessage { get; set; }

public SelectList listOfCounties = Initialise.countiesSelectList;

public Premise premises { get; set; }

}

前提是我数据库中的实体/表。

该表单包含“前提”表中的字段。

在我的javascript函数中,我这样做:

var premisesViewModel = {

Id: 0,

PremisesDescription: $('#premises_PremisesDescription').val(),

OrdnanceSurveyReference: $('#premises_OrdnanceSurveyReference').val(),

PartRestrictedNotes: $('#premises_PartRestrictedNotes').val(),

NatureOfPremises: $('#premises_NatureOfPremises').val(),

AddressLine1: $('#premises_AddressLine1').val(),

AddressLine2: $('#premises_AddressLine2').val(),

Town: $('#premises_Town').val(),

CountyId: $('#premises_CountyId').val(),

Postcode: $('#premises_Postcode').val()

}

alert(form.serialize);

$.ajax({

url: form.attr('action'),

type: 'POST',

dataType: "json",

contentType: 'application/json',

data: JSON.stringify(premisesViewModel),

success: function (data) {

alert('done');

}

})

但是,当我在方法中检查viewModel参数时,它为null:

[HttpPost]

public JsonResult Create(PremisesViewModel pvm)

{

return null;

}

关于如何映射此视图以便正确绑定视图模型的任何想法。谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值