使用部分视图创建MVC表单,每个部分视图都具有不绑定到主模型的复杂模型

时间:2015-01-28 02:18:31

标签: asp.net-mvc partial-views model-binding mvc-editor-templates

我正在处理一个将主模型传递给视图的表单。该模型中包含子模型,具有部分视图以呈现该内容。问题是,当我填写表单时,只有主表单上的那些参数在提交表单时才会被绑定回模型。

我尝试将Html.RenderPartial更改为Html.EditorFor,虽然它修复了我的模型绑定问题,但它从部分视图中删除了我的所有html格式。

有没有办法可以将部分视图元素绑定到主窗体模型,或者使用EditorFor保留部分视图的html结构?

下面是我的代码(我删除了一些东西 - 特别是从我的主视图中 - 试图简化我正在寻找的东西)。

这是我的模特:

public class ShipJobs
{
    public String Job { get; set; }
    public String Quote { get; set; }
    public String PartName { get; set; }
    public String Rev { get; set; }
    public String Customer { get; set; }
    public String CustomerName { get; set; }
    public String TrackingNumber { get; set; }
    public Int32 ShippedQuantity { get; set; }
    public Boolean Certs { get; set; }
    public Double ShippingCharges { get; set; }
    public DateTime ShipDate { get; set; }
    public String SelectedFreightTerms { get; set; }
    public IEnumerable<SelectListItem> FreightTerms { get; set; }
    public String SelectedContact { get; set; }
    public IEnumerable<SelectListItem> Contacts { get; set; }
    public String SelectedShipVia { get; set; }
    public IEnumerable<SelectListItem> ShipVia { get; set; }
    public Models.GreenFolders.Address Address { get; set; }
}

public class Address
{
    public AddressType Type { get; set; }
    public String ShipToId { get; set; }
    public String ContactName { get; set; }
    public String AddressName { get; set; }
    public String Line1 { get; set; }
    public String Line2 { get; set; }
    public String City { get; set; }
    public String State { get; set; }
    public String Zip { get; set; }

    public String Phone { get; set; }
    public SelectList ShipToAttnDropDown { get; set; }
    public IEnumerable<SelectListItem> ShipToDropDown { get; set; }
}

控制器:

public ActionResult ShipJobs(String Job, Models.Shipping.ShippingModel.ShipJobs Packlist, Models.GreenFolders.Address ShipAddress, String Submit = "")
{
    var Model = new Models.Shipping.ShippingModel.ShipJobs();

    if (Submit == "loadjob")
    {
        var shippingHelper = new BLL.Shipping.ShippingMethods(_company);
        Model = shippingHelper.GetShipJobModel(Job);
        Model.Address = shippingHelper.GetShipAddress(Job);
    }
    else if (Submit == "createpacklist")
    {

    }

    ViewBag.Company = _company.ToString();
    return View(Model);
}

主要观点:

@model Models.Shipping.ShippingModel.ShipJobs

@{
    ViewBag.Title = "ShipJobs";
    String Company = ViewBag.Company.ToString();
}

@using (Html.BeginForm("ShipJobs", "Shipping", FormMethod.Post, new { Class = "form-horizontal" }))
{
<div class="row">
    <div class="col-md-6">

        <!-- Basic Form Elements Block -->
        <div class="block">
            <!-- Basic Form Elements Title -->
            <div class="block-title">
                <h2>Load <strong>Job</strong></h2>
            </div>
            <!-- END Form Elements Title -->
            <!-- Basic Form Elements Content -->
            @using (Html.BeginForm("ShipJobs", "Shipping", FormMethod.Post, new { Class = "form-horizontal form-bordered" }))
            {
                <div class="form-group">
                    <label class="col-md-3 control-label" for="example-text-input">Job Number</label>
                    <div class="col-md-9">
                        @Html.TextBoxFor(model => model.Job, new { id = "example-text-input", Name = "Job", Class = "form-control" })
                    </div>
                </div>
                <div class="form-group form-actions">
                    <div class="col-md-9 col-md-offset-3">
                        <button type="submit" class="btn btn-sm btn-primary" name="submit" value="loadjob"><i class="fa fa-angle-right"></i> Load Job Info</button>
                        <button type="reset" class="btn btn-sm btn-warning"><i class="fa fa-repeat"></i> Reset</button>
                    </div>
                </div>
            }
        </div>


    </div>
    <div class="col-md-6">
        @if (Model.Address != null && Model.Address != null)
        {
            @Html.EditorFor(model => model.Address)
            //Html.RenderPartial("../Shared/_Address", Model.ShipInfo);
        }


    </div>
    @Html.HiddenFor(model => model.Quote)
    @Html.HiddenFor(model => Company)
</div>
}

部分观点:

@model Models.GreenFolders.Address

<!-- Block -->
<div class="block">
    <div class="block-title">
        @if(Model.Type == Models.GreenFolders.AddressType.Shipping)
        {
            <h2 style="float: right; margin-top: -9px; margin-right: -10px;">
                <div class="dropdown shiptoddl">
                    <button class="btn btn-default dropdown-toggle" type="button" id="shiptoddl" data-toggle="dropdown" aria-expanded="true">
                        @Model.ShipToDropDown.Where(x => x.Selected).FirstOrDefault().Text
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                        @foreach (SelectListItem selectlistitem in Model.ShipToDropDown)
                        {
                            <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-value="@selectlistitem.Value" data-selected="@selectlistitem.Selected">@selectlistitem.Text</a></li>
                        }
                    </ul>
                </div>
                @*@Html.DropDownList("shiptoddl", (SelectList)Model.ShipToDropDown, new { @class = "shiptoddl", id = "shiptoddl" })*@
            </h2>
        }
        <h4><strong>@Model.Type.ToString()</strong> Address</h4>
    </div>
    @{ Html.RenderPartial("../Shared/_AddressDetails", Model); }
</div>
<!-- END Block -->

0 个答案:

没有答案