好的,我是MVC的新手,我正在解决一个我无法找到答案的问题。
我试图使用jQuery将我的FormModel对象和rowId传递给我的控制器中的Action方法。 rowId按预期方式通过,但FormModel通过null或所有属性设置为默认值。
我认为它与MVC 3绑定机制有关,但对MVC来说是新手,我无法100%确定。
这是Action方法的一个非常简化的版本:
[HttpPost]
public ActionResult RemoveProductMessage(FormModel form, int rowId)
{
try
{
if(Request.IsAjaxRequest())
{
}
}
catch
{
return Redirect("~/DefaultExceptionPage.htm");
}
return PartialView("_ProductMessage", form);
}
以及我用来尝试传递值的JavaScript:
$(document).ready(
(function () {
$("#remove-product-message-row").live(
"click",
function (rowId) {
var url = '/Forms/RemoveProductMessage';
var x = $('#main-form').serialize();
var dataToSend = { form: JSON.stringify(x), rowId: 1 };
$.ajax({
url: "/Forms/RemoveProductMessage",
type: 'POST',
data: $.toDictionary(dataToSend),
success: function (result) {
$("#all-product-messages").html(result);
}
}); // End of $.ajax call
});
}));
我尝试了一些不同的东西,我现在正在尝试这里找到的jQuery插件(jquery.todictionary),但没有运气。我并不依赖于使用此插件以及任何其他方式来使这件事工作,我愿意接受。
我已尝试过很多关于$('#main-form')。serialize()的各种事情,但都无济于事。任何帮助将不胜感激。
感谢。
已修改以添加详细信息
好的,我已经看过Firebug和POST的另一个动作方法,它只将FormModel作为参数看起来像这样:
AdministrationFormNumber 5809
BflBaseFormModel.BaseForm ... 5809
BflBaseFormModel.BaseForm ... 8/1/2010 12:00:00 AM
BflBaseFormModel.BflFormB ... S_5809DYN_082010
BflBaseFormModel.BflFormB ... S_5809DYN_082010
BflBaseFormModel.BflFormB ... DYN
BflBaseFormModel.BflFormB ...代理
BflBaseFormModel.BflFormB ... AS
BflBaseFormModel.BflFormB ... GE
BflBaseFormModel.BflFormB ... LM
BflBaseFormModel.BflFormB ... OC
BflBaseFormModel.BflFormB ...有效
BflBaseFormModel.BflOblig ...
BflBaseFormModel.BondSubT ...
BflBaseFormModel.BondType ... 6
BflBaseFormModel.StateNam ...马萨诸塞州
BflBaseFormModel.Title Transient /巡回供应商 - MA
CreatedDate 3/15/2012 2:39:36 PM
FormNumber 05809
Product.Attachments false
Product.AvailableOnline
Product.AvailableOnline
Product.BondAmtMax 0.00
Product.BondAmtMin 0.00
Product.BondAmtStd
Product.BondEffectiveDate
Product.BondExpirationDat ...
Product.BondObligationDes ...
Product.BondTermMonths
Product.BondType
Product.CancelDays
Product.CategoryId 0
Product.ClassCode
Product.ContactEmail
Product.ContactName
Product.ContactPhone
Product.ContactUrl
Product.CounterSignatures
产品描述
Product.EandOFlag false
Product.EarliestTermEffec ... 1/1/0001 12:00:00 AM
Product.EndorsementFileNa ...
Product.FormId 0
Product.FormTitle
Product.MinCreditScore 0
Product.ObligationType
Product.Obligee.ObligeeNa ...
Product.ProductLineId
Product.ProductVersionAva ... 1/1/0001 12:00:00 AM
Product.ProductVersionExp ... 1/1/0001 12:00:00 AM
Product.RateCode
Product.RateType�
Product.RenewalFileName
Product.RenewalMethod
Product.RiskType
Product.SpecialInstructio ...
Product.State
QiFormModel.Attachment N.
QiFormModel.Description Massachusettes联邦 - 瞬态/巡回供应商债券
QiFormModel.DigitalSignat ......错
QiFormModel.EffDateWrappe ......是的
QiFormModel.FileName LMS_15809_082010.xdp
QiFormModel.FormNumber LMS_15809 / 8/2010
RowID b6c0c011-6153-4391-be0d-7bb7c106372c
StateOfEntity已添加
但是,当我查看RemoveProductMessage的POST时,我看到以下内容:
表格AdministrationFormNumber = 5809& FormNumber = 05809& RowID = b6c0c011-6153-4391-be0d-7bb7c106372c& CreatedDate = 3%2F15%2F2012 + 2%3A39%3A36 + PM& StateOfEntity =已添加& BflBaseFormModel.Title =瞬态% 2FItinerant +卖方+ - + MA&安培; BflBaseFormModel.BaseFormID = 5809&安培; BflBaseFormModel.BaseFormRevisionDate = 8%2F1%2F2010 + 12%3A00%3A00 + AM&安培; BflBaseFormModel.BondTypeId = 6&安培; BflBaseFormModel.BondSubTypeId =安培; BflBaseFormModel.StateName =马萨诸塞&安培; BflBaseFormModel .BflObligeeName =安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.PostedCompanyModels = AS&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.PostedCompanyModels = GE&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.PostedCompanyModels = LM&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.PostedCompanyModels = OC&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.LibraryName =剂&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FormFileType = DYN&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FileName = S_5809DY N_082010&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.StatusInLevel =活动&安培; QiFormModel.FormNumber = LMS_15809%2F + 8%2F2010&安培; QiFormModel.Description = +麻州联合体+ + - +瞬态%2FItinerant +卖方+债券&安培; QiFormModel.DigitalSignatureWrapped =假&安培; QiFormModel.EffDateWrapped =真&安培; QiFormModel.Attachment = N&安培; QiFormModel.FileName = LMS_15809_082010.xdp&安培; BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FileName = S_5809DYN_082010&安培; Product.FormId = 0&安培; Product.FormTitle =安培; Product.AvailableOnline =安培; Product.EarliestTermEffectiveDate = 1%2F1%2F0001 + 12%3A00%3A00 + AM&安培; Product.ProductVersionAvailabilityDate = 1%2F1%2F0001 + 12%3A00%3A00 + AM&安培; Product.ProductVersionExpirationDate = 1%2F1%2F0001 + 12%3A00 %3A00 + AM&安培; Product.Attachments =假安培; Product.Description =安培; Product.RiskType =安培; Product.RateType =%EF%BF%BD&安培; Product.EandOFlag =假安培; Product.BondEffectiveDate =安培; Product.BondExpirationDate = &安培; Product.BondTermMonths =安培; Product.CancelDays =安培; PROD uct.RenewalFileName =安培; Product.RenewalMethod =安培; Product.EndorsementFileName =安培; Product.BondAmtStd =安培; Product.BondAmtMin = 0.00&安培; Product.BondAmtMax = 0.00&安培; Product.MinCreditScore = 0&安培; Product.SpecialInstructions =安培; Product.State =安培; Product.ProductLineId =安培; Product.BondType =安培; Product.CategoryId = 0&安培; Product.AvailableOnline =安培; Product.ClassCode =安培; Product.RateCode =安培; Product.CounterSignatures =安培;产品。 BondObligationDescription =安培; Product.ObligationType =安培; Product.ContactName =安培; Product.ContactPhone =安培; Product.ContactEmail =安培; Product.ContactUrl =安培; Product.ProductMessages%5B0%5D.MsgCode =%24 +++&安培; Product.ProductMessages%5B0%5D.MessageToAddress = 1235&安培; Product.ProductMessages%5B0%5D.StateOfEntity =&添加安培; Product.ProductMessages%5B0%5D.MessageRowID = cae73ecf-4bc9-45f2-bd5d-4040dc7362ef&安培; Product.Obligee.ObligeeName =
似乎未在键/值配对中正确设置值。
模型对象:
public class FormModel : BaseModel
{
private ProductModel productModel;
public BflBaseFormModel BflBaseFormModel
{
get;
set;
}
public QiFormModel QiFormModel
{
get;
set;
}
[DisplayName("Form Number")]
public string FormNumber
{
get;
set;
}
public int AdministrationFormNumber
{
get;
set;
}
public System.Guid RowID
{
get;
set;
}
public System.DateTime CreatedDate
{
get;
set;
}
[AutoMapper.IgnoreMap]
public string Information
{
get;
set;
}
[AutoMapper.IgnoreMap]
public ProductModel Product
{
get
{
if (productModel == null)
{
productModel = new ProductModel();
}
return productModel;
}
set
{
productModel = value;
}
}
public void SetFormModels()
{
// Map Companies to BflForms
foreach (var bflFormByLibraryModels in BflBaseFormModel.BflFormByLibraryModels)
{
if (bflFormByLibraryModels.BflFormModels != null)
{
bflFormByLibraryModels.BflFormModels.Clear();
}
else
{
bflFormByLibraryModels.BflFormModels = new List<BflFormModel>();
}
foreach (var selectedCompany in bflFormByLibraryModels.PostedCompanyModels)
{
var bflFormModel = new BflFormModel();
bflFormModel.CompanyID = selectedCompany;
bflFormModel.BaseFormID = bflFormByLibraryModels.BaseFormID;
bflFormModel.BaseFormRevisionDate = bflFormByLibraryModels.BaseFormRevisionDate;
bflFormModel.LibraryID = bflFormByLibraryModels.LibraryID;
bflFormByLibraryModels.BflFormModels.Add(bflFormModel);
}
}
// Verify that QILatest has been set correctly
if (QiFormModel != null && QiFormModel.AdministrationFormNumber == null)
{
QiFormModel = null;
}
}
}
视图相当大,由各种部分视图组成,因此我不会在此处发布。
好的,我已经在这里做了一条不同的路径,并以另一种方式获取数据,所以一切都很好。
答案 0 :(得分:0)
使用stringify方法传递这两个参数,如下所示
var dataToSend = { form: JSON.stringify(x), rowId: JSON.stringify(1) };