我有一个jQuery对话框。除了下拉列表之外,所有字段都正确发布,该值将作为null而不是所选值传递。
<div id="popupCreateCompany" title="Create a new company">
<form>
<fieldset>
<p>
<label for="company_name">Company Name:</label>
<%= Html.TextBox("company_name") %>
</p>
<p>
<label for="company_desc">Company Description:</label>
<%= Html.TextBox("company_desc") %>
</p>
<p>
<label for="address">Address:</label>
<%= Html.TextBox("address") %>
</p>
<p>
<label for="city">City:</label>
<%= Html.TextBox("city") %>
</p>
<p>
<label for="state">State:</label>
<%= Html.TextBox("state") %>
</p>
<p>
<label for="zip">Zip:</label>
<%= Html.TextBox("zip") %>
</p>
<p>
<label for="website">Website:</label>
<%= Html.TextBox("website", "http:/") %>
</p>
<p>
<label for="sales_contact">Sales Contact:</label>
<%= Html.DropDownList("sales_contact", Model.SelectSalesContacts, "** Select Sales Contact **") %>
</p>
<p>
<label for="primary_company">Primary Company:</label>
<%= Html.DropDownList("primary_company", Model.SelectPrimaryCompanies, "** Select Primary Company **") %>
</p>
</fieldset>
</form>
jQuery的:
$('#popupCreateCompany').dialog(
{
autoOpen: false,
modal: true,
buttons:
{
'Add': function() {
var dialog = $(this);
var form = dialog.find('input:text');
$.post('/company/create', $(form).serialize(), function() {
dialog.dialog('close');
})
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$("#create-company").click(function() {
$('#popupCreateCompany').dialog('open');
});
我的SelectList定义:
public class SubcontractFormViewModel
{
public subcontract Subcontract { get; private set; }
public SelectList SelectPrimaryCompanies { get; set; }
public MultiSelectList SelectService_Lines { get; private set; }
public SelectList SelectSalesContacts { get; private set; }
public SubcontractFormViewModel(subcontract subcontract)
{
SubcontractRepository subcontractRepository = new SubcontractRepository();
Subcontract = subcontract;
SelectPrimaryCompanies = new SelectList(subcontractRepository.GetPrimaryCompanies(), "company_id", "company_name");
SelectService_Lines = new MultiSelectList(subcontractRepository.GetService_Lines(), "service_line_id", "service_line_name", subcontractRepository.GetSubcontractService_Lines(Subcontract.subcontract_id));
SelectSalesContacts = new SelectList(subcontractRepository.GetContacts(), "contact_id", "contact_name");
}
}
答案 0 :(得分:3)
你的问题就在这一行:
var form = dialog.find('input:text');
您只是序列化<input>
个元素,而不是其他表单元素。
您可以通过将其更改为
来添加选择元素var form = dialog.find('input:text, select');
或
var form = dialog.find('input:text').add('select');
答案 1 :(得分:0)
您需要确保对话框仍然在要发布的值的表单中,默认情况下不是这样:
$('#popupCreateCompany').dialog({
autoOpen: false,
modal: true,
buttons:
{
'Add': function() {
var dialog = $(this);
var form = dialog.find('input:text');
$.post('/company/create', $(form).serialize(), function() {
dialog.dialog('close');
})
},
'Cancel': function() {
$(this).dialog('close');
}
}
}).parent().appendTo("#myFormID");
默认情况下,jQuery UI(以及其他一些模式)附加到<body>
末尾的创建模态,因此实际的<select>
元素位于表单之外,这意味着它不包含在内在POST值中。如果您像上面那样执行.parent().appendTo()
,它会将对话框移回到表单中,并且应该正确发布。