I am using MVC, I populate some values before redirecting to an action, During this my dictionary always has a count of 0?

I check the count before it passes and its over 0 but always 0 when it comes through the other side.

I pass other string values too but they all come through fine.

My Redirect to action.

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

My Action result receiving the values.

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 )

Could someone please tell me how i can prevent this.

有帮助吗?

解决方案

I think the dictionary doesn't get serialized. As explained 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:

So save your dictionary into TempData or the Session object.

usage:

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


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

HttpContext.Session Property

ControllerBase.TempData Property

其他提示

try this

    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});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top