为什么模型绑定器会混淆ID字段?

时间:2014-06-02 06:57:03

标签: c# asp.net-mvc model-binding

我有一个用于添加新地址的模型,ShopId位于HiddenForID0的视图中,也是FK Shopnull,因此其中未设置ID

public int ID { get; set; }
public string ShopId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string District { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public int CountryId { get; set; }
public System.DateTime CreationTimestamp { get; set; }
public bool IsPrimary { get; set; }
public Nullable<double> Latitude { get; set; }
public Nullable<double> Longitude { get; set; }

public virtual Country Country { get; set; }
public virtual Shop Shop{ get; set; }

当我发布表单时,ModelState.IsValid显示为false,表示ShopId字段中ID的值无效:

  

值'd04bf59f-be29-4896-ae70-54432b02aa46'对于ID无效。

为什么模型绑定会将ShopId放入ID字段?

这是视图:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ShopAddress</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m => Model.ShopId)
        <div class="form-group">
            @Html.LabelFor(model => model.AddressLine1, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.AddressLine1, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.AddressLine1)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AddressLine2, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.AddressLine2, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.AddressLine2)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AddressLine3, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.AddressLine3, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.AddressLine3)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.District, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.District, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.District)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Region, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Region, new { @class = "form-control" })    
                @Html.ValidationMessageFor(model => model.Region)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PostalCode, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.PostalCode, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PostalCode)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Country, "Country", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CountryId, (IEnumerable<SelectListItem>)ViewBag.Countries, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Country)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.IsPrimary, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.RadioButtonFor(model => model.IsPrimary, "true") Yes
                @Html.RadioButtonFor(model => model.IsPrimary, "false") No
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

2 个答案:

答案 0 :(得分:0)

很可能,binder试图尽力找到id的值,并且因为它没有在视图中显示,所以它将采用名称中包含“id”的字段。尝试为id添加隐藏字段或创建此视图的模型,而不是传递域对象

答案 1 :(得分:0)

我终于解决了这个问题,ShopId被放入ID字段的原因是因为ID是从网址中获取的,D'哦!!!!

页面网址是 / shops / add-address / d04bf59f-be29-4896-ae70-54432b02aa46 然后在HTTPPOST上,模型绑定器非常正确地获取了id(guid)并将其移动了模型ID属性的约定。

相反,我做了我应该做的事情,并创建了一个viewModel,然后使用autoMapper将对象映射到对象。

希望这有助于将来。