如何获取全局声明的列表项 [英] How to get my globally declared list items
问题描述
主要目标是从两个单词生成一个代码。
现在我想获取我的全局声明列表中的项目列表使用该列表的方法我比较输入string.plz帮助我
当我从视图列表中传递我的字符串时,我不会面对空...
我尝试过:
Main aim is Generate One Code From Two Words.
now i want to get list of items in globally declared list in my method with that list i compare input string.plz help me
no i facing when ever i pass my string from view list should be empty...
What I have tried:
my method
public ActionResult Generateid(string Fname)
{
list = new List<string>();
string s1 = Fname;
string s2 = "mpm";
string x = s1.Substring(0, 1);
string y = s2.Substring(0, 1);
string z = s1.Substring(1, 1);
string c = x + y + z;
string s = "00";
int number = Convert.ToInt32(s);
number += 1;
string str1 = number.ToString("D2");
c += str1;
list = new List<string>();
list.Add(c);
Session["sring"] = c;
return Json(c,JsonRequestBehavior.AllowGet);
}
我的课程和页面加载
my class and pageload
public class Test1Controller : Controller
{
public static List<string> list;
//
// GET: /Test1/
#region Test1
public ActionResult Index()
{
return View();
}
推荐答案
如果你想维护一个全球清单,你为什么要扔掉每次你打电话给那个方法时都会有一切,不是一次,而是两次?
一开始,你丢弃任何现有的清单:
If you are trying to maintain a "global list", why are you throwing away everything in it every time you call that method, not once, but twice?
At the beginning, you discard any existing list:
list = new List<string>();
string s1 = Fname;
...
然后当你构建了一个字符串放入其中时,你将它重新丢弃:
Then when you have built a string to put in it, you discard it all over again:
...
c += str1;
list = new List<string>();
list.Add(c);
...
TBH,看起来你在猜测,从随机地方复制代码而不试图理解它,并希望它神奇地在所有工作中结束(缩进是一个很大的线索,生成字符串的代码也是如此 - 那里有很多冗余)。
那不行:坐下来,想想你是什么正在努力做,并设计一个解决方案,而不是希望和祈祷它将起作用。如果你想要一个全局列表,那么你想在一个位置创建它一次,然后根据需要添加和删除项目。
TBH, it looks like you are guessing, copying code from random places without trying to understand it, and hoping it will magically all work in the end (The indentation is a big clue here, as is the code which generates the string - there is a lot of redundancy there).
That won't work: sit down, think about what you are trying to do, and design a solution instead of hoping and praying it will work. If you want a "global list", then you want to create it in one location, once only, and add and remove items as needed from there.
在课堂上:
In class:
public class Test1Controller : Controller
{
static List<string> list;
// GET: /Test1/
#region Test1
static Test1Controller() {
list = new List<string>();
}
功能
Function
public ActionResult Generateid(string Fname)
{
try
{
string s1 = Fname;
string s2 = "mpm";
string x = s1.Substring(0, 1);
string y = s2.Substring(0, 1);
string z = s1.Substring(1, 1);
string c = x + y + z;
string s = "00";
int number = Convert.ToInt32(s);
number += 1;
string str1 = number.ToString("D2");
c += str1;
list.Add(c);
Session["sring"] = list;
return Json(c, JsonRequestBehavior.AllowGet);
}
catch(Exception)
{
throw;
}
不,我现在尝试这样工作吧。 ..
感谢所有...
No i am Trying like this now it working ...
thanks for all...
这篇关于如何获取全局声明的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!