2015-10-07 77 views
0

我有一個表單,允許用戶填寫字段以創建票證(支持票證)。這些字段映射到名爲TicketModel的模型。現在,我有兩個按鈕,一個創建票據(提交表單)和另一個(創建並關閉票據),打開一個對話框,並允許用戶在票證關閉時輸入更多信息。具體來說,他們正在輸入開始日期和結束日期,並描述了他們爲解決問題所做的工作。我無法在對話框中看到模型屬性

創建按鈕正常工作。但是當我點擊對話框中的關閉票據時,我注意到當我的CreateTicket動作方法被調用時,在對話框中填寫的字段的模型屬性(StartDate,EndDate和Description)不在TicketModel參數中(他們爲空)。

以下是我的相關代碼示例。

的CreateTicket管窺

@using (Ajax.BeginForm("AjaxCreateTicket", "Home", 
new { id = "CreateTicketForm", enctype = "multipart/form-data" }, 
new AjaxOptions 
{ 
    OnBegin = "OnBegin", 
    OnSuccess = "OnSuccess", 
    OnFailure = "OnFailure" 
})) 
{ 
@Html.AntiForgeryToken() 
@Html.ValidationSummary(false, "Please correct the following errors:") 
<fieldset> 

    <legend>TicketModel</legend> 

     <div class="editor-label"> 
     @Html.LabelFor(model => model.UserCreated) 
    </div> 
    <div class="editor-field"> 
     @Html.TextBoxFor(model => model.UserCreated) 
    </div> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.UserDisplayName) 
    </div> 
    <div class="editor-field"> 
     @Html.TextBoxFor(model => model.UserDisplayName) 
    </div> 
// Remaining form fields omitted for brevity 

</fieldset> 

<div id="closeDialog" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable" style="display: none;"> 
    @Html.Partial("_CloseNewTicketComment", Model) 
</div> 

} 

這裏是被放置在關閉對話框

@model HelpDesk.Web.Models.TicketModel 

<script> 
// wire up datetimepicker 
$(function() { 
    $(".date-picker").datetimepicker({ 
     timepicker: true, 
     formatDate: 'm/d/Y', 
     formatTime: 'h:i a', 
     format: 'm/d/y h:i a', 
     validateOnBlur: false, 
     scrollInput: false, 
     step: 15 
    }); 
}); 


</script> 

@Html.ValidationSummary(false, "Please correct the following errors:") 
<div class="label"> 
Comment: 
</div> 
<div style="clear:both;height:10px" /> 
<div class="editor-field" id="closeTextBox"> 
@Html.TextAreaFor(model => model.CommentModel.Description, new { @id =  "CloseDescription" }) 
</div> 
<div style="clear:both;height:10px" /> 

<div class="editor-label"> 
@Html.LabelFor(model => model.DateWorkStarted) 
</div> 
<div class="editor-field"> 
@Html.TextBoxFor(model => model.DateWorkStarted, new { @class = "date-picker", @style = "width: 160px;" }) 
</div> 
<div style="clear:both; height:10px" /> 

<div class="editor-label"> 
@Html.LabelFor(model => model.DateClosed) 
</div> 
<div class="editor-field"> 
@Html.TextBoxFor(model => model.DateClosed, new { @class = "date-picker",  @style = "width: 160px;" }) 
</div> 

<div> 
@Html.HiddenFor(model => model.UserCreated) 
</div> 
<div style="clear:both; height:10px" /> 

我的javascript從CreateTicket局部視圖

<script type="text/javascript"> 
var userLookupData = ''; 
var thisRedactorCreate = ''; 

// wire up datetimepicker 
$(function() { 
    $(".date-picker").datetimepicker({ 
     timepicker: true, 
     formatDate: 'm/d/Y', 
     formatTime: 'h:i a', 
     format: 'm/d/y h:i a', 
     validateOnBlur: false, 
     scrollInput: false, 
     step: 15 
    }); 
}); 

function OnBegin() { 
progressDialogOpen(); 
} 

function OnSuccess() { 
//thisRedactorCreate.redactor("destroy"); 
progressDialogClose(); 
$('#CreateTicketForm').clearForm(); 
showSuccessDialog(0); 
} 

function OnFailure() { 
progressDialogClose(); 
} 

// This is the click event jquery that opens the dialog 
    $("#CreateCloseButton").click(function() { 

     // change IsCreateAndClose boolean because this ticket is going to be closed 
     // upon creation 
     $("#IsCreateAndClose").val("true") 

     if ($('form').valid()) 
     { 
      $(".validation-summary-errors") 
      .removeClass("validation-summary-errors") 
      .addClass("validation-summary-valid"); 


      $("#closeDialog").dialog({ 
       width: 525, 
       height: 500, 
       modal: true, 
       resizable: true, 
       title: 'Close Ticket', 
       close: function() { 
        $(this).dialog('close'); 
       }, 
       buttons:{ 
        "Close Ticket": function() { 
         if ($('form').valid()) 
         { 
          $('form').submit(); 
          $('#closeDialog').dialog("close"); 
         } 
        }, 
        "Cancel": function() { 
         $(this).dialog('close'); 
        } 
       } 
      }); 
     } 
    }) 

的ActionMethod的partialView那在ajax呼叫

 [ValidateInput(false)]//this is necessary because we are posting HTML 
     [ValidateAntiForgeryToken] 
     public ActionResult AjaxCreateTicket(TicketModel ticketModel) 
     { 

      // If this action is not being called from the create and close dialog 
      // we need to remove the data annotations for the descriptiong property of the commmentModel. 
      if (!ticketModel.IsCreateAndClose) 
      { 
       ModelState.Remove("CommentModel.Description"); 
      } 


      if (ModelState.IsValid) 
      { 
       bool ticketCreated = false; 

       try 
       { 

        Ticket ticket = new Ticket(); 

        //form collection 
        ticket.Title = ticketModel.Title; 
        ticket.UserEmail = ticketModel.UserEmail; 
        ticket.Description = ticketModel.Description; 
        ticket.UserComputerName = ticketModel.UserComputerName; 
// Irrelevant code omitted for brevity. 

如果我在action方法中設置了一個斷點並將一個手錶添加到TicketModel對象。我注意到,commentModel中的DateWorkStarted,DateClosed和Description字段爲空。過去幾天我一直在四處尋找,試圖找出爲什麼沒有運氣。有沒有人有任何想法,我可能做錯了(或根本不做)?

+0

你的按鈕在哪裏? – Yoky

+0

你能告訴我們TicketModel的代碼嗎? – JDupont

+0

你可以發佈有問題的輸入生成的HTML嗎? – JB06

回答

0

從我在代碼表單中可以看到的內容正確地'提交',儘管表單的定義被省略了,並且不清楚它在哪裏發佈。也許它發佈到AjaxCreateTicket操作,儘管我沒有看到用[HttpPost]裝飾的這個動作?

如果是這樣,它看起來像ModelBinder有問題。 (不幸的是你的模型也沒有顯示)。這就是爲什麼我只能懷疑你的模型定義了公共領域而不是公共屬性。 MopelBinder只處理屬性並忽略字段,不能訪問它們 - public,internal,protected。私人物品根本不算。

當然,我可能完全錯了,因爲你的代碼中缺少重要的部分。如果你把你的模型作爲最低限度發佈,它會對發生的事情有更多的瞭解。

0

經過幾天與同事合作,我們能夠得到它的工作。唯一的問題是,我不記得我們做了什麼來完成它的工作(在很多晚上的時間裏指責它),但它現在似乎正在工作。

感謝您對此的所有意見。

相關問題