2016-02-19 107 views
1

我有一個包含兩個局部視圖的視圖。一個選擇一個客戶,另一個創建一個新客戶,這兩個客戶都顯示在父頁面的標籤中。如果有人選擇了一個客戶,然後繼續使用數據輸入表單,但是必須返回創建一個新客戶,那麼所選客戶的數據仍然存在。我可以通過輸入新數據來替換數據,但重要的是客戶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

您使用的是部分,這意味着HTML已經加載並呈現時,它到達客戶端。

要處理標識的情況下被髮送,你應該標記..Customer.CustomerIddisabled="disabled"readonly,這將避免在提交Input tag,這將導致服務器端「想」(如果你在實現它的客戶端)這是一個新的客戶。

您可以使用jQuery 1.6+輕鬆實現它:

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

禁用於< 1.6:Disable/enable an input with jQuery?

+0

於是點擊了新的客戶標籤時,我禁用隱藏字段包含客戶ID,將停止編號被返回到服務器。然後當選擇選擇客戶標籤時,我重新啓用它? – DaRoGa

+0

這似乎不工作。我在我的標籤上onclick上創建的,所以這被禁用點擊新的客戶選項卡,但ID仍然返回到控制器。我已經在瀏覽器中調試了javascript,並且我知道onclick函數被稱爲 – DaRoGa

+0

100%它可以工作,爲HTML CustomerId標記添加「disabled =」disabled「硬編碼,並且看不到它將被髮送到服務器。 –