字典作为参数传递时会丢失值?

时间:2014-05-13 13:48:02

标签: c# asp.net-mvc dictionary

我正在使用MVC,我在重定向到一个动作之前填充了一些值,在此期间我的字典总是有一个0?

我在计数通过之前检查计数,并且在通过另一侧时总是为0,但是它总是为0。

我也传递了其他字符串值,但它们都很好。

我的重定向行动。

return RedirectToAction(
    "APNewQuote", 
    new { 
        OENumber = "", QuoteNumber = "", ClaimNumber = ClaimNumber,
        MotorBodyRepairer = "", VehicleRegistration = vRegistration,
        vehicleMakeId = "", vehicleModelId = "", vehicleRangeId = "",
        vehicleMakeModels = "", vehicleModelCode = "",
        year = vYear, OEParts = OEParts }
);

我的行动结果接收值。

public ActionResult APNewQuote(string OENumber, string QuoteNumber, string ClaimNumber,
    string MotorBodyRepairer, string VehicleRegistration, int? vehicleMakeId,
    int? vehicleModelId, int? vehicleRangeId, string vehicleMakeModels,
    string vehicleModelCode, int? year, Dictionary<string,string> OEParts )

有人可以告诉我如何防止这种情况。

2 个答案:

答案 0 :(得分:1)

我认为字典没有被序列化。正如in this question's answer If you need to pass in some-what complex objects to an action after a redirect, you probably want to use either a Session or TempData:

所述

将字典保存到TempDataSession对象。

用法:

// save
TempData["myDictionary"] = OEParts;


// retrieve
var myDic = TempData["myDictionary"] as Dictionary<string,string>;

HttpContext.Session Property

ControllerBase.TempData Property

答案 1 :(得分:-1)

试试这个

    var oem = new Dictionary<string, string>();
        DataTable dt = DataAccessLayer.GetPartsDamaged()
        foreach(DataRow row in dt.Rows)
        {
            oem.Add(row["key"].ToString(),row["value"].ToString());
        }

    return RedirectToAction("APNewQuote", new { OENumber = "", QuoteNumber = "", ClaimNumber = ClaimNumber, MotorBodyRepairer = "", VehicleRegistration = vRegistration, vehicleMakeId = "", vehicleModelId = "", vehicleRangeId = "", vehicleMakeModels = "", vehicleModelCode = "", year = vYear, OEParts = oem});