当作为一个参数传递时,字典会失去价值?[英] Dictionary loses value when passed as a parameter?

本文是小编为大家收集整理的关于当作为一个参数传递时,字典会失去价值?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我正在使用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 )

有人可以告诉我我如何预防.

推荐答案

我认为字典不会序列化.正如解释的那样在这个问题的答案中

因此,将您的字典保存到TempData或Session对象中.

用法:

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


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

/p>

contranterbase.tempdata属性

其他推荐答案

尝试此

    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});

本文地址:https://www.itbaoku.cn/post/2045351.html

问题描述

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});