如何将值从一个动作传递给具有相同控制器的另一个动作 [英] How to pass value from one action to another action having same controller
本文介绍了如何将值从一个动作传递给具有相同控制器的另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在MVC3中开发应用程序.我被困在一个地方.每次控制进入IIndex1
动作时,其参数值都变为0.但它应与IIndex
动作参数中的值相同.我已经使用了会话,ViewBag,ViewData,但是我的问题仍然存在.请建议我.
Hi I am developing an application in MVC3. and i am stuck at one place. Everytime when control goes to IIndex1
action its argument value has become 0. But it should be same as value in IIndex
action argument. I have used session, ViewBag, ViewData but my problem is remains. Please suggest me.
public ActionResult GetMDN(string msisdn)
{
number = msisdn.Substring(0, msisdn.IndexOf('$'));
if (number.ToLower() != "unknown" && number.Length == 12)
{
number = number.Remove(0, 2);
}
Session["msdresponse"] = number;
Session["moptr"] = msisdn.Substring(msisdn.LastIndexOf('$') + 1);
number = msisdn;
int sngid=int.Parse(ViewData["isongid"].ToString());
return RedirectToAction("IIndex1", new { iid = sngid });
}
public ActionResult IIndex(int id)
{
ViewBag.isongid = id;
ViewData["isongid"] = id;
Response.Redirect("http:XXXXXXXXXXXXXXXX");
return RedirectToAction("GetMDN");
}
public ActionResult IIndex1(int iid)
{
}
推荐答案
您可以使用TempData.您可以将各种类型的数据传递到操作之间,无论它们是否在同一控制器中.您的代码应类似于:
You can use TempData.You can pass every types of data between to action, whether they are in same controller or not. Your code should be something like it:
public ActionResult GetMDN(string msisdn)
{
int sngid=10;
TempData["ID"] = sngid;
return RedirectToAction("IIndex");
}
public ActionResult IIndex()
{
int id = Convert.ToInt32(TempData["ID"]);// id will be 10;
}
这篇关于如何将值从一个动作传递给具有相同控制器的另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文