我有一個表單,允許用戶填寫字段以創建票證(支持票證)。這些字段映射到名爲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字段爲空。過去幾天我一直在四處尋找,試圖找出爲什麼沒有運氣。有沒有人有任何想法,我可能做錯了(或根本不做)?
你的按鈕在哪裏? – Yoky
你能告訴我們TicketModel的代碼嗎? – JDupont
你可以發佈有問題的輸入生成的HTML嗎? – JB06