0

我不知道我做错了什么。好吧,我有这个表格,它是复杂视图的一部分。

    @{
    var filtersAjaxOptions = new AjaxOptions
        {
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "clientList-body",
            OnBegin = "clientList.filterRequestStart()",
            OnComplete = "clientList.filterRequestComplete()",
            OnSuccess = "clientList.filterRequestSuccess()"
        };
    }
    <span class="clientFilters-filterValue inlineBlock">
        @using (Ajax.BeginForm(
            "Index",
            "ClientList",
            new {
                ProductId = Model.ClientListViewModel.Filters.ProductId,
                ClientFilter = Model.ClientListViewModel.Filters.ClientFilter,
                BillFilter = Model.ClientListViewModel.Filters.BillFilter,
                DateSortType = Model.ClientListViewModel.Filters.DateSortType,
                SortDirection = Model.ClientListViewModel.Filters.SortDirection
            },
            filtersAjaxOptions,
            new
            {
                id = "clientListDateFilter-form"
            }
        ))
        {
            @Html.TextBoxFor(
                m => m.ClientListViewModel.Filters.BeginDateRange,
                new
                {
                    @class = "dp-input textInput inlineBlock",
                    id = "dp-billDateFilterStart",
                }
            )
            @Html.TextBoxFor(
                m => m.ClientListViewModel.Filters.EndDateRange,
                new
                {
                    @class = "dp-input textInput inlineBlock",
                    id = "dp-billDateFilterEnd",
                }
            )
        }
    </span>

这是过滤器模型

    public class FilterModel
    {
        public FilterModel()
        {
            ClientFilter = ClientsEnum.All;
            BillFilter = ClientBillsEnum.All;
        }

        public string ProductId { get; set; }
        public ClientsEnum ClientFilter { get; set; }
        public ClientBillsEnum BillFilter { get; set; }
        public DateTime? BeginDateRange { get; set; }
        public DateTime? EndDateRange { get; set; }
        public DateSortType? DateSortType { get; set; }
        public SortDirection? SortDirection { get; set; }
    }

这部分是ClientListController方法索引

    public ActionResult Index(FilterModel filters)
    {
        var clientListViewModel = GetClientListViewModel(filters, 1, 1, PageSize);
        if (ControllerContext.HttpContext.Request.IsAjaxRequest())
            return PartialView("Partial/ClientListBody", clientListViewModel);
        return View(clientListViewModel);
    }

每当我提交上面的表格时,我发现“BeginDateRange”和“EndDateRange”字段为空,其他字段设置正确。虽然,当我在 Watch 中插入Request.Form时,我可以看到整个数据。

更新 1 所以我将 Web.config 中的 <globalisation> 设置为:

    <globalisation responseHeaderEncoding="utf-8" culture="en-US">

但它不起作用。结果和之前非常相似。

更新 2 此外,当我尝试将所有路由值数据放入 @Html.HiddenFor 时,控制器只看到空值。再一次,Request.Form 已填写完毕。

所以问题是:如何将表单数据绑定到传入模型?泰

4

2 回答 2

1

绑定日期时间时,默认模型绑定器使用当前区域性日期时间格式。这意味着您必须在文本框中输入正确格式的日期。另一方面,如果您需要固定格式,则可以在 web.config(<globalization>元素)中使用固定文化或编写自定义模型绑定器:https ://stackoverflow.com/a/7836093/29407


更新:

您需要指定正确的绑定前缀,因为您的输入字段被命名为,ClientListViewModel.Filters.BeginDateRange但您的控制器操作采用FilterModelas 参数而不是根视图模型:

public ActionResult Index([Bind(Prefix = "ClientListViewModel.Filters")] FilterModel filters)
{
    ...
}

但是现在这会破坏其他值,因此您还需要调整视图:

@using (Ajax.BeginForm(
    "Index",
    "ClientList",
    null,
    filtersAjaxOptions,
    new
    {
        id = "clientListDateFilter-form"
    }
 ))
{
    @Html.HiddenFor(x => x.ClientListViewModel.Filters.ProductId)
    @Html.HiddenFor(x => x.ClientListViewModel.Filters.ClientFilter)
    @Html.HiddenFor(x => x.ClientListViewModel.Filters.BillFilter)
    @Html.HiddenFor(x => x.ClientListViewModel.Filters.DateSortType)
    @Html.HiddenFor(x => x.ClientListViewModel.Filters.SortDirection)

    @Html.TextBoxFor(
        m => m.ClientListViewModel.Filters.BeginDateRange,
        new
        {
            @class = "dp-input textInput inlineBlock",
            id = "dp-billDateFilterStart",
        }
    )
    @Html.TextBoxFor(
        m => m.ClientListViewModel.Filters.EndDateRange,
        new
        {
            @class = "dp-input textInput inlineBlock",
            id = "dp-billDateFilterEnd",
        }
    )
}

或者如果您想将它们作为表单 url 的一部分发送,而不是使用隐藏字段:

@using (Ajax.BeginForm(
    "Index",
    "ClientList",
    new RouteValueDictionary 
    { 
        { "ClientListViewModel.Filters.ProductId", Model.ClientListViewModel.Filters.ProductId },
        { "ClientListViewModel.Filters.ClientFilter", Model.ClientListViewModel.Filters.ClientFilter },
        { "ClientListViewModel.Filters.BillFilter", Model.ClientListViewModel.Filters.BillFilter },
        { "ClientListViewModel.Filters.DateSortType", Model.ClientListViewModel.Filters.DateSortType },
        { "ClientListViewModel.Filters.SortDirection", Model.ClientListViewModel.Filters.SortDirection },
    },
    filtersAjaxOptions,
    new RouteValueDictionary
    {
        { "id", "clientListDateFilter-form" }
    }
 ))
{
    @Html.TextBoxFor(
        m => m.ClientListViewModel.Filters.BeginDateRange,
        new
        {
            @class = "dp-input textInput inlineBlock",
            id = "dp-billDateFilterStart",
        }
    )
    @Html.TextBoxFor(
        m => m.ClientListViewModel.Filters.EndDateRange,
        new
        {
            @class = "dp-input textInput inlineBlock",
            id = "dp-billDateFilterEnd",
        }
    )
}
于 2012-08-24T06:47:49.027 回答
0

试试这个:

public ActionResult Index(FilterModel filters, FormCollection collection)
{
    UpdateModel(filters, "ClientListViewModel");
    var clientListViewModel = GetClientListViewModel(filters, 1, 1, PageSize);
    if (ControllerContext.HttpContext.Request.IsAjaxRequest())
        return PartialView("Partial/ClientListBody", clientListViewModel);
    return View(clientListViewModel);
}

并认为:

@Html.TextBoxFor(
            m => m.ClientListViewModel.FilterModel.EndDateRange,
            new
            {
                @class = "dp-input textInput inlineBlock",
                id = "dp-billDateFilterEnd",
            }
        )

你有奇怪的名字。此外,最好使用隐藏字段然后通过路由值传递值。

于 2012-08-24T06:46:31.903 回答