当用户按下一个按钮时,如何增加变量值? [英] How do I increament the value of variable on each time when user press next button ?
本文介绍了当用户按下一个按钮时,如何增加变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int id = 1 ;
public PartialViewResult ViewQuestions()
{
string constr = ConfigurationManager.ConnectionStrings [ DefaultConnection]。ToString();
GetQuestions getquest = new GetQuestions();
try
{
SqlConnection con = new SqlConnection(constr) ;
con.Open();
SqlCommand cmd = new SqlCommand( 选择q.QuestID,q.QImage,q.Question,o.FOption,o.SOption,o.TOption,o.Fruption from QuestionSet as q join OptionSet as o on q.QuestID = o.QuestID where q.QuestID=@questid ,con);
cmd.Parameters.AddWithValue( @ questid,id);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
getquest.QuestID = dr.GetInt32( 0 跨度>);
getquest.QuestImage = dr.GetString( 1 );
getquest.Question = dr.GetString( 2 );
dr.Close();
SqlCommand cmd1 = new SqlCommand( 选择Foption,Soption,Toption,来自Optionset的FRoption,其中QuestID = @ questid,con);
cmd1.Parameters.AddWithValue( @ questid,id);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
List< Options> opt = new 列表< Options>();
opt.Add( new 选项{Option1 = dr1.GetString( 0 )});
opt.Add( new 选项{Option1 = dr1.GetString( 1 )});
opt.Add( new 选项{Option1 = dr1.GetString( 2 )});
opt.Add( new 选项{Option1 = dr1.GetString( 3 )});
getquest.Options = opt;
}
dr1.Close();
}
}
catch (例外情况)
{
}
return PartialView(getquest);
}
[HttpPost]
public RedirectToRouteResult Forward()
{
int ID = Convert.ToInt32(Request.Form [ QuestID 跨度>]);
id = ID + 1 ;
return RedirectToAction( ViewQuestions跨度>);
}
我的尝试:
每次用户在视图页面按下一步按钮时,我都会尝试更新id的值。为此分配post方法Forward?
提前致谢。
解决方案
Pass id作为ViewQuestion()方法的参数。
并且在ex = ach上单击调用ViewQuestion(int id),id =递增值。
将id保留在层后面的代码中
int id=1;
public PartialViewResult ViewQuestions()
{
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
GetQuestions getquest = new GetQuestions();
try
{
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("Select q.QuestID,q.QImage,q.Question,o.FOption,o.SOption,o.TOption,o.FROption from QuestionSet as q join OptionSet as o on q.QuestID=o.QuestID where q.QuestID=@questid ", con);
cmd.Parameters.AddWithValue("@questid", id);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
getquest.QuestID = dr.GetInt32(0);
getquest.QuestImage = dr.GetString(1);
getquest.Question = dr.GetString(2);
dr.Close();
SqlCommand cmd1 = new SqlCommand("Select Foption,Soption,Toption,FRoption from Optionset where QuestID=@questid ", con);
cmd1.Parameters.AddWithValue("@questid", id);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
List<Options> opt = new List<Options>();
opt.Add(new Options { Option1 = dr1.GetString(0) });
opt.Add(new Options { Option1 = dr1.GetString(1) });
opt.Add(new Options { Option1 = dr1.GetString(2) });
opt.Add(new Options { Option1 = dr1.GetString(3) });
getquest.Options = opt;
}
dr1.Close();
}
}
catch (Exception ex)
{
}
return PartialView(getquest);
}
[HttpPost]
public RedirectToRouteResult Forward()
{
int ID = Convert.ToInt32(Request.Form["QuestID"]);
id = ID + 1;
return RedirectToAction("ViewQuestions");
}
What I have tried:
I try to update the value of id each time when user press the "Next" button from view page . I assign the post method "Forward" for that?
Thanks in advance.
解决方案
Pass id as argument to the ViewQuestion() method.
And on ex=ach click call ViewQuestion(int id) with id= incremented value.
Keep id in code behind layer
这篇关于当用户按下一个按钮时,如何增加变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文