0

当我提交表单时,这些值没有绑定到视图模型。我看不出我做错了什么。有没有人在我的代码中看到我遗漏的东西。

这是视图的一部分:

@model FacilitiesCostIndex.ViewModels.AddAsset.TheViewModel

<div id="addAssetForm-div">
@using (Html.BeginForm("Submit", "AddAsset", FormMethod.Post, new { id = "AddAssetForm" }))
{
<span class="formTitle">@ViewBag.Title</span><br />

<fieldset id="topRow">
    <legend></legend>
    <!--Location-->
    <span id="location" class="notes">- Location -</span><br />
    <!--Site-->
    <span id="site-span">
        @Html.LabelFor(model => model.SiteId, new { @class = "formLabel" })
        @Html.DropDownListFor(model => model.SiteId, Model.SiteItems, new { id = "siteDrpDwn", @class = "formDropDown" })
        @Html.ValidationMessageFor(model => model.SiteId, string.Empty, new { id = "siteVldMsg", @class = "formValidationMessage" })
    </span>
    <!--Floor-->
    <span id="floor-span">
        @Html.LabelFor(model => model.FloorId, new { @class = "formLabel" })
        <span id="floorDrpDwnContainer" data-populateurl="@Url.Action("PopulateFloorDrpDwn", "AddAsset")" data-unpopulateurl="@Url.Action("UnpopulateFloorDrpDwn", "AddAsset")">
            @{Html.RenderPartial("AddAsset/UnpopulatedFloorDrpDwn", new FacilitiesCostIndex.ViewModels.AddAsset.FloorDrpDwnViewModel());}
        </span>
        @Html.ValidationMessageFor(model => model.FloorId, string.Empty, new { id = "floorVldMsg", @class = "formValidationMessage" })
    </span>
    <!--Zone-->
    <span id="zone-span">
        @Html.LabelFor(model => model.ZoneId, new { @class = "formLabel" })
        <span id="zoneDrpDwnContainer" data-populateurl="@Url.Action("PopulateZoneDrpDwn", "AddAsset")" data-unpopulateurl="@Url.Action("UnpopulateZoneDrpDwn", "AddAsset")">
            @{Html.RenderPartial("AddAsset/UnpopulatedZoneDrpDwn", new FacilitiesCostIndex.ViewModels.AddAsset.ZoneDrpDwnViewModel());}
        </span>
        @Html.ValidationMessageFor(model => model.ZoneId, string.Empty, new { id = "zoneVldMsg", @class = "formValidationMessage" })
    </span><br />
</fieldset>

<fieldset id="leftColumn">
    <legend></legend>
    <!--Install Date-->
    <div id="installDate-div" class="leftColumnItem">
        @Html.LabelFor(model => model.InstallDate, new { id="installDateLbl" }) <br />
        @Html.TextBoxFor(model => model.InstallDate, new { id="installDateTxt" })
    </div>

    <!--Name-->
    <div id="assetName-div" class="leftColumnItem">
        @Html.LabelFor(model => model.AssetName, new { @class="formLabel" }) <br />
        @Html.TextBoxFor(model => model.AssetName, new { id="assetNameTxt", @class = "formTextbox" })
    </div>
    <!--Asset Number-->
    <div id="assetNumber-div" class="leftColumnItem">
        @Html.LabelFor(model => model.AssetNumber, new { @class="formLabel" }) <br />
        @Html.TextBoxFor(model => model.AssetNumber, new { id="assetNumberTxt", @class = "formTextbox" })
    </div>
    <!--Description-->
    <div id="description-div" class="leftColumnItem">
        @Html.LabelFor(model => model.Description, new { @class="formLabel" }) <br />
        @Html.TextBoxFor(model => model.Description, new { id="descriptionTxt", @class = "formTextbox" })
    </div>
    <!--Group-->
    <div id="group-div" class="leftColumnItem">
        @Html.LabelFor(model => model.GroupId, new { @class = "formLabel" }) <br />
        @Html.DropDownListFor(model => model.GroupId, Model.GroupItems, new { id = "groupDrpDwn", @class = "formDropDown" })
        @Html.ValidationMessageFor(model => model.GroupId, string.Empty, new { id = "groupVldMsg", @class = "formValidationMessage" })
    </div>

这是 ViewModel 的一部分:

public class TheViewModel
{
    [Display(Name = "Site")]
    public int SiteId { get; set; }
    public IEnumerable<SelectListItem> SiteItems { get; private set; }

    [Display(Name = "Floor")]
    [Required(ErrorMessage = "Please select a Floor.")]
    public int FloorId { get; set; }

    [Display(Name = "Zone")]
    [Required(ErrorMessage = "Please select a Zone.")]
    public int ZoneId { get; set; }

    [Display(Name = "Name")]
    [Required(ErrorMessage = "Please enter an Asset Name.")]
    public string AssetName { get; set; }

    [Display(Name = "Asset Number")]
    [Required(ErrorMessage = "Please enter an Asset Number.")]
    public string AssetNumber { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "INSTALL DATE")]
    [Required(ErrorMessage = "Please enter an install date.")]
    public DateTime? InstallDate { get; set; }

    [Display(Name = "Group")]
    [Required(ErrorMessage = "Please select a Group.")]
    public int GroupId { get; set; }
    public IEnumerable<SelectListItem> GroupItems { get; private set; }

    public TheViewModel()
    {
        SiteId = -1;
        SetSiteItems();
        GroupId = -1;
        SetGroupItems();
    }

这是控制器的代码:

    [HttpPost]
    public ActionResult Submit(TheViewModel model)
    {
        DateTime DateEntered = DateTime.Now;

        AssetModel AnAsset = new AssetModel();
        AnAsset.AssetName = model.AssetName;
        AnAsset.AssetNumber = model.AssetNumber;
        AnAsset.AssetTypeId = model.AssetTypeId;
        AnAsset.GroupId = model.GroupId;
        AnAsset.ZoneId = model.ZoneId;
        AnAsset.Description = model.Description;

        //Note: Conversion from nullable to non-nullable type is dealt with.
        if (model.InstallDate.HasValue)
        {
            AnAsset.InstallDate = model.InstallDate.Value;
        }

        //Send asset to insert method.
        new AssetRepositoryModel().Insert(AnAsset);

        return View();
    }

感谢您能给我的任何帮助。谢谢。

4

1 回答 1

0

可能使用 Global.asax 中的绑定并定义从 IModelBinder 继承的 TheViewModelBinder 类来解决?

ModelBinders.Binders.Add( typeof(TheViewModel), new TheViewModelBinder() );

和活页夹类是这样的:

public class TheViewModelBinder : IModelBinder {
    public object BindModel( ControllerContext controllerContext, ModelBindingContext     bindingContext ) {
                    var theViewModel = new TheViewModel();
                    theViewModel.SiteId = int.Parse(request.Form.Get( "SiteId" ));
                    //other your code here
            }
    }
于 2014-02-21T00:14:27.593 回答