单个属性没有得到约束上HttpPost [英] Single property not getting bound on HttpPost

查看:108
本文介绍了单个属性没有得到约束上HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的第一个项目MVC3在我们公司,我已经打了一个块。看起来没人能够弄清楚发生了什么事情。

I'm working on the first MVC3 project at our company, and I've hit a block. No one can seem to figure out what's going on.

我有我在页面上使用一个复杂的模型:

I have a complex Model that I'm using on the page:

public class SpaceModels : List<SpaceModel> {
    public bool HideValidation { get; set; }
    [Required(ErrorMessage=Utilities.EffectiveDate + Utilities.NotBlank)]
    public DateTime EffectiveDate { get; set; }

    public bool DisplayEffectiveDate { get; set; }
}

在控制器中,我创建一个SpaceModels空白SpaceModels对象时,空间得到了相结合(这将是目标空间)。

In the Controller, I create a SpaceModels object with blank SpaceModels for when Spaces get combined (this would be the destination Space).

// Need a list of the models for the View.
SpaceModels models = new SpaceModels();
models.EffectiveDate = DateTime.Now.Date;
models.DisplayEffectiveDate = true;
models.Add(new SpaceModel { StoreID = storeID, SiteID = siteID, IsActive = true });

        return View("CombineSpaces", models);

然后在视图中,我使用的SpaceModels对象作为模型,并在窗体正在为生效日期一个TextBox:

Then in the View, I am using that SpaceModels object as the Model, and in the form making a TextBox for the Effective Date:

@model FiveBelow.CoTenancy.Data.SpaceModels

@using (Html.BeginForm("CombineSpaces", "Space")) {
    <div class="EditLine">
        <span class="EditLabel LongText">
            New Space Open Date
        </span>
        @Html.TextBoxFor(m => m.EffectiveDate, new {
                        size = "20",
                        @class = "datecontrol",
                        // Make this as a nullable DateTime for Display purposes so we don't start the Calendar at 1/1/0000.
                        @Value = Utilities.ToStringOrDefault(Model.EffectiveDate == DateTime.MinValue ? null : (DateTime?)Model.EffectiveDate, "MM/dd/yyyy", string.Empty)
        })
        @Html.ValidationMessageFor(m => m.EffectiveDate)
    </div>

    <hr />        

    Html.RenderPartial("_SpaceEntry", Model);
}

局部视图,通过所有SpaceModels得到渲染迭代,并创建包含对个人SpaceModel对象编辑字段。 (我使用的列表中使用时,空间得到细分,以及对同一视图。)

The Partial View that gets rendered iterates through all SpaceModels, and creates a containing the Edit fields for the individual SpaceModel objects. (I'm using the List to use the same Views for when the Spaces get Subdivided as well.)

然后在HttpPost,EFFECTIVEDATE的仍是回到它的默认DateTime.MinValue:

Then on the HttpPost, the EffectiveDate is still back at it's DateTime.MinValue default:

[HttpPost]
public ActionResult CombineSpaces(SpaceModels model, long siteID, long storeID, DateTime? effectiveDate) {
// processing code
}

我添加了日期时间? EFFECTIVEDATE参数来证明,当它被改变的价值其实回来呢。我甚至尝试移动文本框到_SpaceEntry局部视图的渲染,但是毫无效果有两种。

I added that DateTime? effectiveDate parameter to prove that the value when it gets changed does in fact come back. I even tried moving the rendering of the TextBox into the _SpaceEntry Partial View, but nothing worked there either.

我也尝试使用 @ Html.EditorFor(M = GT; m.EffectiveDate)代替了 @ Html.TextBoxFor( ),但仍然返回DateTime.MinValue。 (我的老板不喜欢放弃使用 @ Html.EditorForModel 的方式呈现的控制。)

I did also try using the @Html.EditorFor(m => m.EffectiveDate) in place of the @Html.TextBoxFor(), but that still returned DateTime.MinValue. (My boss doesn't like giving up the control of rendering using the @Html.EditorForModel by the way.)

必须有一些简单的我失踪。请让我知道你需要什么。

There has to be something simple that I'm missing. Please let me know if you need anything else.

推荐答案

纵观<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/e7526daecf9d#src/System.Web.Mvc/DefaultModelBinder.cs\"相对=nofollow>来源$ C ​​$ C为 DefaultModelBinder ,具体 BindComplexModel(),如果它检测一个集合键入它会结合各个元件,但将不会试图绑定列表对象本身的性质。

Looking at the source code for DefaultModelBinder, specifically BindComplexModel(), if it detects a collection type it will bind the individual elements but will not attempt to bind properties of the list object itself.

这篇关于单个属性没有得到约束上HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆