Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC DropDownList重定向到Eidt视图_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC DropDownList重定向到Eidt视图

C# ASP.NET MVC DropDownList重定向到Eidt视图,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我是ASP.NETMVC新手,如果我的问题太简单,请原谅 我需要创建一个下拉列表,列出员工的姓和名。当用户从下拉列表中选择员工时,“我的网站”应将用户重定向到允许用户编辑记录的编辑视图 我在控制器中的代码如下所示: public ActionResult Index(string sortOrder, string searchString) { ViewBag.NameList = (from e in db.Employee select e.FirstName + ",&

我是ASP.NETMVC新手,如果我的问题太简单,请原谅

我需要创建一个下拉列表,列出员工的姓和名。当用户从下拉列表中选择员工时,“我的网站”应将用户重定向到允许用户编辑记录的编辑视图

我在控制器中的代码如下所示:

public ActionResult Index(string sortOrder, string searchString)
{
    ViewBag.NameList = (from e in db.Employee select e.FirstName + "," + e.LastName);
    return View(employee.ToList());
}

<p>
    Fast way: @Html.DropDownList("Edit",  new SelectList(ViewBag.NameList))
    @Html.ActionLink("Edit", "Edit", new SelectList(ViewBag.NameList));
</p>
我的观点是这样的:

public ActionResult Index(string sortOrder, string searchString)
{
    ViewBag.NameList = (from e in db.Employee select e.FirstName + "," + e.LastName);
    return View(employee.ToList());
}

<p>
    Fast way: @Html.DropDownList("Edit",  new SelectList(ViewBag.NameList))
    @Html.ActionLink("Edit", "Edit", new SelectList(ViewBag.NameList));
</p>
上面的代码没有像我预期的那样工作。无法重定向该页。也许我还需要在DropDownList中绑定项目的ID?有人能帮我实现这个功能吗?

你好

首先,您有一个操作结果,其中列出了

  public ActionResult Index()
    {

        ViewBag.Emp = new SelectList(( from s in db.Employee.OrderBy(a =>a.Forename)
                                           select new {Id = s.EmployeeID, Name = 
s.Forename +  " - " + s.Surname }),
                                           "Id","Name");

        return View();
    }
那么你的观点是这样的

<h2>Index</h2>

@using (Html.BeginForm("Edit","Employees",FormMethod.Get))
{

@Html.Label("Employee")
@Html.DropDownList("id",(SelectList)ViewBag.Emp)  
<br />
<input type="submit" value="Edit" />
}

就这么简单

这是使用下拉列表中的选择时重定向到编辑视图的示例代码。 如前所述,您只需更改视图。创建一个Javascript函数提交此表单,然后在dropdownlist更改时调用该函数

这是代码。如果这个问题解决了你的问题,请投票表决

@model IEnumerable<Incendo.Entities.Employee>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Edit", "Employees", FormMethod.Get, new { id = "TransPost" }))
{

@Html.Label("Employee")
@Html.DropDownList("id", (SelectList)ViewBag.Emp, " ", new {@onchange = "postTrans()" 
})
<br />
<input type="submit" value="Edit" />
}


@section scripts{
<script>
    function postTrans() {
        
        document.getElementById("TransPost").submit();
    }
</script>



}
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@使用(Html.BeginForm(“Edit”、“Employees”、FormMethod.Get、new{id=“TransPost”}))
{
@Html.Label(“员工”)
@Html.DropDownList(“id”,(SelectList)ViewBag.Emp,”,新的{@onchange=“postTrans()
})

} @节脚本{ 函数postTrans(){ document.getElementById(“TransPost”).submit(); } }
Hi@Wen。如果需要重定向而不单击按钮,则需要插入javascript。有了按钮点击会容易得多。你喜欢哪种方法?嗨@YatFeiLeong谢谢你的评论。我更喜欢点击按钮。但是如果可以,你能介绍这两种方法吗?如果没有,请介绍单击按钮的方法。非常感谢,是的!谢谢你的帮助!如果是,你能介绍另一种方法吗?这是一种不需要按钮的方法。相同的引号,但创建一个javascript,在下拉列表的值更改期间调用。如果能将这种方法用于教育目的,它肯定不适合商业用途。请尽快编写代码。非常感谢。我尝试了两种方法,它们都很好。谢谢你让我学到了新知识。如果它对你有用,请投票作为答案。我会的,但我对stackflow是新手。所以我没有足够的声誉。我只能表明答案已被接受。但当我有足够的声誉时,我会回来投票给你的答案。再次感谢你的帮助。