未在模型中设置 DropDownListFor 的选定值
Selected value for DropDownListFor not being set in model
我已经搜索了很多小时并尝试了所有的建议。我想我缺少一些基本概念。我想使用模型而不是 ViewBag
,并且我在 post 背面的模型中设置了 selected item/value。
我正在 cshtml 中设置 ddl 和操作 link 如下:
@model VinExampleProject.Models.MembersModel
@Html.DropDownListFor(n => n.SearchColumn, new List<SelectListItem>
{
new SelectListItem {Text = "Select A Column", Value = "1" },
new SelectListItem {Text = "UserName", Value = "2" },
new SelectListItem { Text = "FirstName", Value = "3" },
new SelectListItem { Text = "LastName", Value = "4" }
}
, "Select A Column")
@Html.ActionLink("Search", "Search", Model)
控制器有这个方法
public ActionResult Search(MembersModel mModel)
{
// mModel.SearchColumn always 0 or empty string (i have tried both types in the model)
return RedirectToAction("Index");
}
我希望 mModel.SearchColumn
设置为 select item/value,但始终为空。
这是我的模型:
using System.Collections.Generic;
public class MembersModel
{
// public enum SearchColumns { UserName, FirstName, LastName };
///<summary>
/// Gets or sets Customers.
///</summary>
public List<Member> Members { get; set; }
///<summary>
/// Gets or sets CurrentPageIndex.
///</summary>
public int CurrentPageIndex { get; set; }
///<summary>
/// Gets or sets PageCount.
///</summary>
public int MembersPerPage { get; set; }
///<summary>
/// Gets or sets column to sort by.
///</summary>
public string SortColumn { get; set; }
/// <summary>
/// Total amount of actib=ve mambers
/// </summary>
public int ActiveMemberCount { get; set; }
/// <summary>
/// Column to search
/// </summary>
public int SearchColumn { get; set; } // also have tried string here
/// <summary>
/// Value to search for
/// </summary>
public string SearchValue { get; set; }
}
我猜当您到达 Search
动作时,不仅 SearchColumn
是空的,而且您模型的所有其他属性也是空的。要获取从表单发送到您的操作的值,您需要将它们放入表单并提交表单。
@model VinExampleProject.Models.MembersModel
@using (Html.BeginForm("Search", "Search", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.DropDownListFor(m => m.SearchColumn, Model.SearchColumnList, "Select A Column")
<input type="submit" value="SEARCH" />
}
请注意,我还为您添加了 AntiForgeryToken
,建议这样做以保护您的网站。我还建议将您的项目列表移至您的模型:
public class MembersModel {
public IEnumerable<SelectListItem> SearchColumnList { get; set; }
}
并且您的操作必须具有 ValidateAntiForgeryToken
和 HttpPost
属性。您将在 HttpGet
操作中生成列表项作为模型的一部分:
public ActionResult Search() {
var model = new MembersModel();
model.SearchColumnList = new List<SelectListItem> {
new SelectListItem {Text = "Select A Column", Value = "1" },
new SelectListItem {Text = "UserName", Value = "2" },
new SelectListItem { Text = "FirstName", Value = "3" },
new SelectListItem { Text = "LastName", Value = "4" }
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Search(MembersModel model)
{
//Now your model will be populated with the values from the form
}
如果你真的想使用link,那么你应该考虑使用Ajax。您可以使用 jQuery serialize()
函数在 JavaScript 中序列化您的表单字段,然后通过 Ajax 发送它们。像这样:
<a id="SearchLink" href="#">Search</a>
<script>
$('#SearchLink).click(function(e) {
e.preventDefault();
$.post("@Url.Action("Search", "Search"), $("#myForm").serialize(), function(data) {
alert("Data sent successfully!");
});
return false;
});
</script>
或者(可能是更好的方法),可以使表单本身成为 Ajax 表单,因此它将通过 Ajax 提交自己并调用 JavaScript 回调函数完成了:
@model VinExampleProject.Models.MembersModel
@using (Ajax.BeginForm("Search", "Search", null, new AjaxOptions {
OnSuccess = "SearchSuccessCallback(data);",
OnFailure = "SearchFailureCallback();"
})) {
@Html.AntiForgeryToken()
@Html.DropDownListFor(m => m.SearchColumn, Model.SearchColumnList, "Select A Column")
<input type="submit" value="SEARCH" />
}
<script>
function SearchSuccessCallback(data) {
alert("Data sent successfully!");
}
function SearchFailureCallback() {
alert("There was an error!");
}
</script>
我已经搜索了很多小时并尝试了所有的建议。我想我缺少一些基本概念。我想使用模型而不是 ViewBag
,并且我在 post 背面的模型中设置了 selected item/value。
我正在 cshtml 中设置 ddl 和操作 link 如下:
@model VinExampleProject.Models.MembersModel
@Html.DropDownListFor(n => n.SearchColumn, new List<SelectListItem>
{
new SelectListItem {Text = "Select A Column", Value = "1" },
new SelectListItem {Text = "UserName", Value = "2" },
new SelectListItem { Text = "FirstName", Value = "3" },
new SelectListItem { Text = "LastName", Value = "4" }
}
, "Select A Column")
@Html.ActionLink("Search", "Search", Model)
控制器有这个方法
public ActionResult Search(MembersModel mModel)
{
// mModel.SearchColumn always 0 or empty string (i have tried both types in the model)
return RedirectToAction("Index");
}
我希望 mModel.SearchColumn
设置为 select item/value,但始终为空。
这是我的模型:
using System.Collections.Generic;
public class MembersModel
{
// public enum SearchColumns { UserName, FirstName, LastName };
///<summary>
/// Gets or sets Customers.
///</summary>
public List<Member> Members { get; set; }
///<summary>
/// Gets or sets CurrentPageIndex.
///</summary>
public int CurrentPageIndex { get; set; }
///<summary>
/// Gets or sets PageCount.
///</summary>
public int MembersPerPage { get; set; }
///<summary>
/// Gets or sets column to sort by.
///</summary>
public string SortColumn { get; set; }
/// <summary>
/// Total amount of actib=ve mambers
/// </summary>
public int ActiveMemberCount { get; set; }
/// <summary>
/// Column to search
/// </summary>
public int SearchColumn { get; set; } // also have tried string here
/// <summary>
/// Value to search for
/// </summary>
public string SearchValue { get; set; }
}
我猜当您到达 Search
动作时,不仅 SearchColumn
是空的,而且您模型的所有其他属性也是空的。要获取从表单发送到您的操作的值,您需要将它们放入表单并提交表单。
@model VinExampleProject.Models.MembersModel
@using (Html.BeginForm("Search", "Search", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.DropDownListFor(m => m.SearchColumn, Model.SearchColumnList, "Select A Column")
<input type="submit" value="SEARCH" />
}
请注意,我还为您添加了 AntiForgeryToken
,建议这样做以保护您的网站。我还建议将您的项目列表移至您的模型:
public class MembersModel {
public IEnumerable<SelectListItem> SearchColumnList { get; set; }
}
并且您的操作必须具有 ValidateAntiForgeryToken
和 HttpPost
属性。您将在 HttpGet
操作中生成列表项作为模型的一部分:
public ActionResult Search() {
var model = new MembersModel();
model.SearchColumnList = new List<SelectListItem> {
new SelectListItem {Text = "Select A Column", Value = "1" },
new SelectListItem {Text = "UserName", Value = "2" },
new SelectListItem { Text = "FirstName", Value = "3" },
new SelectListItem { Text = "LastName", Value = "4" }
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Search(MembersModel model)
{
//Now your model will be populated with the values from the form
}
如果你真的想使用link,那么你应该考虑使用Ajax。您可以使用 jQuery serialize()
函数在 JavaScript 中序列化您的表单字段,然后通过 Ajax 发送它们。像这样:
<a id="SearchLink" href="#">Search</a>
<script>
$('#SearchLink).click(function(e) {
e.preventDefault();
$.post("@Url.Action("Search", "Search"), $("#myForm").serialize(), function(data) {
alert("Data sent successfully!");
});
return false;
});
</script>
或者(可能是更好的方法),可以使表单本身成为 Ajax 表单,因此它将通过 Ajax 提交自己并调用 JavaScript 回调函数完成了:
@model VinExampleProject.Models.MembersModel
@using (Ajax.BeginForm("Search", "Search", null, new AjaxOptions {
OnSuccess = "SearchSuccessCallback(data);",
OnFailure = "SearchFailureCallback();"
})) {
@Html.AntiForgeryToken()
@Html.DropDownListFor(m => m.SearchColumn, Model.SearchColumnList, "Select A Column")
<input type="submit" value="SEARCH" />
}
<script>
function SearchSuccessCallback(data) {
alert("Data sent successfully!");
}
function SearchFailureCallback() {
alert("There was an error!");
}
</script>