当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc 3 - Passing model and other params to controller in MVC 3

问题描述:

OK, I'm new to MVC and I'm working away on an issue which I can't see to find an answer for.

I'm trying to pass my FormModel object and also a rowId to an Action method in my controller using jQuery. The rowId comes through as expected but the FormModel either comes through as null or with all the properties set to their default values.

I believe it's something to do with the MVC 3 binding mechanism, but being so new to MVC I can't be 100% sure.

Here's a very simplified version of the Action method:

 [HttpPost]

public ActionResult RemoveProductMessage(FormModel form, int rowId)

{

try

{

if(Request.IsAjaxRequest())

{

}

}

catch

{

return Redirect("~/DefaultExceptionPage.htm");

}

return PartialView("_ProductMessage", form);

}

and here's the JavaScript that I'm using to try and pass the values through:

$(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

});

}));

I've tried a few different things and I'm currently trying the jQuery plug-in found here (jquery.todictionary) but with no luck. I'm not tied to using this plug-in and any other way to get this thing working, I'm open to.

I've tried many various things regarding the $('#main-form').serialize(), but all to no avail. Any help would be greatly appreciated.

Thanks.

Edited to add details

Ok, I've taken a look in Firebug and the POST for another action method which only takes the FormModel as a param looks like this:

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... Agent

BflBaseFormModel.BflFormB... AS

BflBaseFormModel.BflFormB... GE

BflBaseFormModel.BflFormB... LM

BflBaseFormModel.BflFormB... OC

BflBaseFormModel.BflFormB... Active

BflBaseFormModel.BflOblig...

BflBaseFormModel.BondSubT...

BflBaseFormModel.BondType... 6

BflBaseFormModel.StateNam... Massachusetts

BflBaseFormModel.Title Transient/Itinerant Vendor - 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.Description

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 Commonwealth of Massachusettes - Transient/Itinerant Vendor Bond

QiFormModel.DigitalSignat... False

QiFormModel.EffDateWrappe... True

QiFormModel.FileName LMS_15809_082010.xdp

QiFormModel.FormNumber LMS_15809/ 8/2010

RowID b6c0c011-6153-4391-be0d-7bb7c106372c

StateOfEntity Added

However, when I look at the POST for the RemoveProductMessage I see the following:

form AdministrationFormNumber=5809&FormNumber=05809&RowID=b6c0c011-6153-4391-be0d-7bb7c106372c&CreatedDate=3%2F15%2F2012+2%3A39%3A36+PM&StateOfEntity=Added&BflBaseFormModel.Title=Transient%2FItinerant+Vendor+-+MA&BflBaseFormModel.BaseFormID=5809&BflBaseFormModel.BaseFormRevisionDate=8%2F1%2F2010+12%3A00%3A00+AM&BflBaseFormModel.BondTypeId=6&BflBaseFormModel.BondSubTypeId=&BflBaseFormModel.StateName=Massachusetts&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=Agent&BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FormFileType=DYN&BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FileName=S_5809DYN_082010&BflBaseFormModel.BflFormByLibraryModels%5B0%5D.StatusInLevel=Active&QiFormModel.FormNumber=LMS_15809%2F+8%2F2010&QiFormModel.Description=Commonwealth+of+Massachusettes+-+Transient%2FItinerant+Vendor+Bond&QiFormModel.DigitalSignatureWrapped=False&QiFormModel.EffDateWrapped=True&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=false&Product.Description=&Product.RiskType=&Product.RateType=%EF%BF%BD&Product.EandOFlag=false&Product.BondEffectiveDate=&Product.BondExpirationDate=&Product.BondTermMonths=&Product.CancelDays=&Product.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=&Product.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=Added&Product.ProductMessages%5B0%5D.MessageRowID=cae73ecf-4bc9-45f2-bd5d-4040dc7362ef&Product.Obligee.ObligeeName=

It appears that the values are not being correctly set in a Key/Value pairing.

The Model Object:

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;

}

}

}

The View is pretty large, made up of various Partial Views so I won't post it on here.

OK, I've gone done a different path here and got the data across another way, so all is good.

网友答案:

Pass the two parameters with the stringify method, like this

var dataToSend = { form: JSON.stringify(x), rowId: JSON.stringify(1) };
分享给朋友:
您可能感兴趣的文章:
随机阅读: