Asp.Net MVC下拉列表选择值消失 [英] Asp.Net MVC dropdownlist selected value dissappears

查看:60
本文介绍了Asp.Net MVC下拉列表选择值消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器,我将数据传递给另一个。当我将数据传递给第一个控制器时,第二个控制器下拉列表的选定值消失。我google了好几个小时但我失败了。当用户点击按钮并刷新页面后,我如何保持选中的ListItem?这是我的代码:



第一个具有选择列表和控制代码的控制器。



第一控制器:< br $>


列表< SelectListItem> list = new List< SelectListItem> 
{
new SelectListItem {Text =---Seçiniz----,Value =},
new SelectListItem {Text =text1,Value =11} ,
new SelectListItem {Text =text2,Value =12},
new SelectListItem {Text =text3,Value =13},
new SelectListItem {Text = text4,Value =14},
new SelectListItem {Text =text5,Value =15}
};

if(TempData [daire]!= null)
{

int daire = Convert.ToInt32(TempData [daire]);
ViewBag.daire = new SelectList(list,Value,Text,TempData [daire]);
model =(folderBL.getAll()。Where(k => k.Id == daire))。ToList();
}
else
{
ViewBag.daire = new SelectList(list,Value,Text,);
model = folderBL.getAll();
}





第二个控制器,我获取SelectList的daire值并将数据发送到第一个控制器。



  public  ActionResult SecondController(string daire)
{
TempData [ daire] = daire;
return RedirectToAction( FirstController);
}





和查看。



 @using(Html.BeginForm(SecondController,MyDashboard,FormMethod.Post))
{
@ Html.DropDownList(daire,(SelectList)ViewBag.daire,new { id =mydrop})

< td>< input type =submitvalue =Send/>
}

解决方案

您可以使用:



@ Html.DropDownList(daire,(SelectList)ViewBag.daire,---Seçiniz----)







@ Html.DropDownListFor(m => m.daire,新的SelectList(ViewBag.daire,Value,Text),---Seçiniz--- - ,new {@id =mydrop})


如果有人需要,这是解决方案。只需更改视图



@using(Html.BeginForm(SecondAction,MyDashboard,FormMethod.Post))

{

@ Html.DropDownList(daire)

< input type =submitvalue =发送/>

}

I have two controllers and i am passing data one to another. When i pass data second controller to first controller dropdown's selected value disappears. I googled many hours but i failed.How can i keep selected ListItem when user clicks button and after page refreshes? Here is my codes:

First controller which has selectlist and controlling codes.

First Controller:

List<SelectListItem> list= new List<SelectListItem>
            {
            new SelectListItem {  Text = "--- Seçiniz----", Value = ""},
            new SelectListItem {  Text = "text1", Value = "11"},
            new SelectListItem { Text = "text2", Value = "12"},
            new SelectListItem {Text = "text3", Value = "13"},
            new SelectListItem {Text = "text4", Value = "14"},
            new SelectListItem { Text = "text5", Value = "15"}
            };

            if (TempData["daire"] != null)
            {

            int daire = Convert.ToInt32(TempData["daire"]);
            ViewBag.daire = new SelectList(list, "Value", "Text", TempData["daire"]);
            model= (folderBL.getAll().Where(k=>k.Id==daire)).ToList();
            }
           else
            {
            ViewBag.daire = new SelectList(list, "Value", "Text","");
            model= folderBL.getAll();
            }



Second controller, I get the daire value for my SelectList and send data to first controller.

public ActionResult SecondController(string daire)
       {
           TempData["daire"] = daire;
           return RedirectToAction("FirstController");
       }



And View.

@using (Html.BeginForm("SecondController","MyDashboard",FormMethod.Post))
          {
           @Html.DropDownList("daire", (SelectList)ViewBag.daire, new { id = "mydrop" })

           <td><input type="submit" value="Send" />
          }

解决方案

You can use :

@Html.DropDownList("daire", (SelectList)ViewBag.daire, "--- Seçiniz----")

or

@Html.DropDownListFor(m => m.daire, new SelectList(ViewBag.daire, "Value", "Text"), "--- Seçiniz----", new { @id = "mydrop" })


Here is the solution if someone needs it. Just change the View

@using (Html.BeginForm("SecondAction", "MyDashboard", FormMethod.Post))
{
@Html.DropDownList("daire")
<input type="submit" value="Send" />
}


这篇关于Asp.Net MVC下拉列表选择值消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆