DropDownList的选定值未在模型中设置

时间:2018-02-24 19:10:13

标签: c# asp.net asp.net-mvc-5 dropdownlistfor

我一直在寻找好几个小时,并尝试了所有的建议。我想我错过了一些基本概念。我想使用的模型不是ViewBag,我在帖子背面的模型中设置了所选的项目/值。

我在cshtml中设置了一个ddl和action链接,如下所示:

@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设置为选择项/值,但始终为空。

这是我的模特:

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; }
}

1 个答案:

答案 0 :(得分:1)

我猜不仅SearchColumn为空,而且当您到达Search操作时,模型的所有其他属性都是空的。要获取从表单发送到您的操作的值,您需要将它们放在表单中并提交表单。

@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; }
}

您的操作必须具有ValidateAntiForgeryTokenHttpPost属性。您将在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
}

如果您真的想使用链接,那么您应该考虑使用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>