重置部分视图的模型值

时间:2016-02-19 09:37:15

标签: c# jquery model-view-controller model

我有一个包含两个局部视图的视图。一个用于选择客户,另一个用于创建新客户,均显示在父页面的选项卡中。如果有人选择了客户,然后继续使用数据输入表单,但是必须返回创建新客户,那么所选客户的数据仍然存在。我可以通过输入新数据来替换数据,但重要的是客户ID仍然存在,因此没有考虑其他数据。有没有办法让我在点击例如点击时重置客户对象一个编辑按钮,而不会丢失页面其余部分的数据?

我的父母观点(相关部分)是:

    <div class="form-group">
        @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-12">
            @if (Model.CommunicationModel.Communication.Customer.Name == null)
            {
                <input id="btnCust" type="button" value="Select" class="btn btn-default selectCustomer" />
            }
            else
            {
                <span id="custSpan" style="font-size:16px; font:bold;">
                    @Html.DisplayFor(model => model.CommunicationModel.Communication.Customer.Name)
                    @Html.HiddenFor(model => model.CommunicationModel.Communication.Customer.CustomerId)
                    <input id="btnEditCust" type="button" value="Edit" class="btn btn-default selectCustomer" />
                </span>
            }
            <div id="customerTabs" style="display:none;">
                <hr />
                <p>
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#tabs-1" aria-controls="tabs-1" role="tab" data-toggle="tab">Select Customer</a></li>
                        <li role="presentation"><a href="#tabs-2" aria-controls="tabs-2" role="tab" data-toggle="tab">Create New Customer</a></li>
                    </ul>
                </p>

                <div class="tab-content">
                    <div id="tabs-1" role="tabpanel" class="tab-pane active">
                        @Html.Partial("~/Views/Communication/SelectCustomer.cshtml", Model.CustomerViewModel.AllCustomers)
                    </div>
                    <div id="tabs-2" role="tabpanel" class="tab-pane">
                        @Html.Partial("~/Views/Communication/CreateCustomer.cshtml", Model)
                    </div>
                </div>
            </div>
        </div>
    </div>

我的创建客户部分视图是:

@model CommunicationsLog.ViewModels.CommunicationViewModel

<div class="form-horizontal" id="addCustomerForm">
<div class="row">
    <div class="col-md-12">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.Name, new { htmlAttributes = new { @class = "form-control" }, @id = "name" })
                @Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.Name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, new { htmlAttributes = new { @class = "form-control" }, @id = "email" })
                @Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.ContactEmail, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.ContactTel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.ContactTel, new { htmlAttributes = new { @class = "form-control" }, @id = "phone" })
                @Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.ContactTel, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.CompanyName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.CompanyName, new { htmlAttributes = new { @class = "form-control" }, @id = "company" })
                @Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.CompanyName, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.CommunicationModel.Communication.Customer.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.CommunicationModel.Communication.Customer.Address, new { htmlAttributes = new { @class = "form-control" }, @id = "address" })
                @Html.ValidationMessageFor(model => model.CommunicationModel.Communication.Customer.Address, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
</div>

任何帮助都会非常感激。我很难过:)

1 个答案:

答案 0 :(得分:1)

您正在使用Partials,这意味着html在到达客户端时已经加载并呈现。

要处理正在发送ID的情况,您应该将..Customer.CustomerId标记为disabled="disabled"readonly,这样可以避免客户端提交Input tag,将是服务器端&#34;思考&#34; (如果你以这种方式实现它)这是一个新客户。

您可以使用jQuery 1.6 +轻松实现它:

$('#TheCustomerId').prop('disabled', true);
$('#TheCustomerId').prop('disabled', false);

禁用&lt; 1.6:Disable/enable an input with jQuery?