0

好的,我是 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.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 瞬态/巡回供应商 - 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 马萨诸塞州联邦 - 临时/流动供应商债券 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 添加

但是,当我查看 RemoveProductMessage 的 POST 时,我看到以下内容:

形成AdministrationFormNumber = 5809&FormNumber = 05809&行ID = 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。库名称=剂&BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FormFileType = DYN&BflBaseFormModel.BflFormByLibraryModels%5B0%5D.FileName = S_5809DYN_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 =&产品.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=已添加&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;
        }
    }
}

视图很大,由各种局部视图组成,所以我不会在这里发布。

好的,我在这里走了一条不同的路,并通过另一种方式获取数据,所以一切都很好。

4

1 回答 1

0

用 stringify 方法传递两个参数,像这样

var dataToSend = { form: JSON.stringify(x), rowId: JSON.stringify(1) };
于 2012-06-08T12:46:30.553 回答