将选定的 DropDownList 值发送到 HomeController ActionResult
Send Selected DropDownList value to HomeController ActionResult
您好,我有一个下拉列表,该列表由配置中的逗号分隔值填充。这很好用。
我想做的是将单击按钮时选择的值发送到 HomeController 中的 ActionResult。
我创建了一个模型,它接受一个字符串。当我按下按钮时出现错误:
The view 'TestAction' or its master was not found or no view engine supports the searched locations.
这是我的控制器的样子:
[HttpPost]
[ActionName("TestAction")]
public ActionResult TestAction(SQL_Blocks_App.Models.DropdownList SelectedValue)
{
//System.Diagnostics.Debug.WriteLine(SelectedValue);
return View();
}
这是我的模型的样子:
public class DropdownList
{
//
// GET: /DropdownList/
[Display(Name = "Servers")]
public string SelectedValue{ get; set; }
}
这就是我的索引视图的样子:
<form id="SelectedValue" action="/Home/TestAction" method="post" style="margin: 0">
<div class="col-lg-5">
@{
ViewBag.Title = "Index";
}
@Html.DropDownList("YourElementName", (IEnumerable<SelectListItem>)ViewBag.DropdownVals, "--Choose Your Value--", new
{
//size = "5",
style = "width: 600px"
})
</div>
<div class="col-lg-5">
<input type="submit" value="Run Query" />
<input id="Button2" type="button" value="Clear" onclick="window.location.reload()" />
</div>
</form>
我想澄清一下。我的最终目标是在 ActionResult 的 SQL 查询中使用所选值,并将结果 return 返回到索引中,以便我可以将它们填充到 table 中。 (你现在不必告诉我如何做 SQL 部分,我只想看到至少在输出中打印的所选值。)
您的 TestAction 方法正在返回一个视图。确保视图 TestAction.cshtml 存在并且位于主文件夹中。
return View()
的默认框架行为是 return 与当前执行的操作同名的视图。即 TestAction
。错误告诉你没有找到这样的视图。
你有几个选择。您可以 创建视图 ,也可以 return 其他内容。例如,如果你想重定向回 Index
那么你可以 return 一个重定向结果:
return RedirectToAction("Index");
您还可以在响应中指定Index
视图:
return View("Index");
但是,请记住 URL 仍然适用于 TestAction
而 而不是 Index
,如果您没有意识到这一点,可能会导致行为发生意外变化。
编辑: 根据对这个答案的评论,听起来你真正想要的是建立一个 对 的动作,通常在同一个视图上操作。这对于 index 视图不是特别常见,但对于 edit 视图非常常见。唯一的区别是语义,从结构上讲,这个概念在任何地方都适用。
考虑两个动作:
public ActionResult Index()
{
// just show the page
return View();
}
[HttpPost]
public ActionResult Index(SQL_Blocks_App.Models.DropdownList SelectedValue)
{
// receive data from the page
// perform some operation
// and show the page again
return View();
}
这两个操作之间的请求仅在 HTTP 动词(GET 或 POST)上有所不同,而不是在 URL 上的操作名称不同。该名称将始终为 "Index"。但是当索引视图上的表单通过 POST 提交并具有 "SelectedValue" 时,将调用第二个操作而不是第一个操作。
在第二个操作中,您将执行数据库交互,收集所需的任何数据,并在必要时在响应中包含模型或一些其他数据。
重定向到索引操作,并传递参数
[HttpPost]
[ActionName("TestAction")]
public ActionResult TestAction(SQL_Blocks_App.Models.DropdownList _selectedValue)
{
//System.Diagnostics.Debug.WriteLine(SelectedValue);
return RedirectToAction("Index", "[Controller]", new {@_selectedValue = _selectedValue });
}
然后您的 Index 方法应该接受该参数。
[HttpGet]
public ActionResult Index(SQL_Blocks_App.Models.DropdownList _selectedValue)
{
//use _selectedValue
}
我建议使用除索引之外的其他方法,或者将 Dropdownlist nullable/set 设置为默认方法。
您好,我有一个下拉列表,该列表由配置中的逗号分隔值填充。这很好用。
我想做的是将单击按钮时选择的值发送到 HomeController 中的 ActionResult。
我创建了一个模型,它接受一个字符串。当我按下按钮时出现错误:
The view 'TestAction' or its master was not found or no view engine supports the searched locations.
这是我的控制器的样子:
[HttpPost]
[ActionName("TestAction")]
public ActionResult TestAction(SQL_Blocks_App.Models.DropdownList SelectedValue)
{
//System.Diagnostics.Debug.WriteLine(SelectedValue);
return View();
}
这是我的模型的样子:
public class DropdownList
{
//
// GET: /DropdownList/
[Display(Name = "Servers")]
public string SelectedValue{ get; set; }
}
这就是我的索引视图的样子:
<form id="SelectedValue" action="/Home/TestAction" method="post" style="margin: 0">
<div class="col-lg-5">
@{
ViewBag.Title = "Index";
}
@Html.DropDownList("YourElementName", (IEnumerable<SelectListItem>)ViewBag.DropdownVals, "--Choose Your Value--", new
{
//size = "5",
style = "width: 600px"
})
</div>
<div class="col-lg-5">
<input type="submit" value="Run Query" />
<input id="Button2" type="button" value="Clear" onclick="window.location.reload()" />
</div>
</form>
我想澄清一下。我的最终目标是在 ActionResult 的 SQL 查询中使用所选值,并将结果 return 返回到索引中,以便我可以将它们填充到 table 中。 (你现在不必告诉我如何做 SQL 部分,我只想看到至少在输出中打印的所选值。)
您的 TestAction 方法正在返回一个视图。确保视图 TestAction.cshtml 存在并且位于主文件夹中。
return View()
的默认框架行为是 return 与当前执行的操作同名的视图。即 TestAction
。错误告诉你没有找到这样的视图。
你有几个选择。您可以 创建视图 ,也可以 return 其他内容。例如,如果你想重定向回 Index
那么你可以 return 一个重定向结果:
return RedirectToAction("Index");
您还可以在响应中指定Index
视图:
return View("Index");
但是,请记住 URL 仍然适用于 TestAction
而 而不是 Index
,如果您没有意识到这一点,可能会导致行为发生意外变化。
编辑: 根据对这个答案的评论,听起来你真正想要的是建立一个 对 的动作,通常在同一个视图上操作。这对于 index 视图不是特别常见,但对于 edit 视图非常常见。唯一的区别是语义,从结构上讲,这个概念在任何地方都适用。
考虑两个动作:
public ActionResult Index()
{
// just show the page
return View();
}
[HttpPost]
public ActionResult Index(SQL_Blocks_App.Models.DropdownList SelectedValue)
{
// receive data from the page
// perform some operation
// and show the page again
return View();
}
这两个操作之间的请求仅在 HTTP 动词(GET 或 POST)上有所不同,而不是在 URL 上的操作名称不同。该名称将始终为 "Index"。但是当索引视图上的表单通过 POST 提交并具有 "SelectedValue" 时,将调用第二个操作而不是第一个操作。
在第二个操作中,您将执行数据库交互,收集所需的任何数据,并在必要时在响应中包含模型或一些其他数据。
重定向到索引操作,并传递参数
[HttpPost]
[ActionName("TestAction")]
public ActionResult TestAction(SQL_Blocks_App.Models.DropdownList _selectedValue)
{
//System.Diagnostics.Debug.WriteLine(SelectedValue);
return RedirectToAction("Index", "[Controller]", new {@_selectedValue = _selectedValue });
}
然后您的 Index 方法应该接受该参数。
[HttpGet]
public ActionResult Index(SQL_Blocks_App.Models.DropdownList _selectedValue)
{
//use _selectedValue
}
我建议使用除索引之外的其他方法,或者将 Dropdownlist nullable/set 设置为默认方法。