有一些問題可以將html數據類型發佈到webapi發佈方法。將數據類型html發佈到ASP.NET WebAPI
這是後置代號
$('form#katalog').submit(function(e) {
e.preventDefault();
var data = {
action: 'katalog-callback',
input: {
Firstname: $('form#katalog #firstname').val(),
Lastname: $('form#katalog #lastname').val(),
Address: $('form#katalog #address').val(),
Zip:$('form#katalog #zip').val(),
City: $('form#katalog #city').val(),
Phone: $('form#katalog #tel').val(),
Mobile: $('form#katalog #mobile').val(),
Email: $('form#katalog #email').val(),
BuildStart: $('form#katalog #build-start').val(),
Plot: $('form#katalog #plot').val(),
BuildCity: $('form#katalog #build-city').val()
}
};
var request = $.ajax({
url: "/ajax",
type: "POST",
data: data,
dataType: "html"
});
request.done(function(result) {
if (result == 1) {
alert('Thx!')
} else {
alert('Error')
}
});
});
這是的WebAPI方法
public string Post(LeadModel lead)
{
try
{
svc.AddLead(lead);
}
catch
{
return "exception";
}
return "true";
這是Model
公共類LeadModel
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Address { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string BuildStart { get; set; }
public string Plot { get; set; }
public string BuildCity { get; set; }
}
這是來自IIS的錯誤消息{「Message」:「出現錯誤 」,「ExceptionMessage」:「沒有MediaTypeFormatter可用於 從媒體類型爲 的內容讀取類型爲'LeadModel'的對象' 「System.Net.Http.HttpContentExtensions.ReadAsAsync [T]」(HttpContent 內容,類型類型,IEnumerable
1 formatters, IFormatterLogger formatterLogger)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable
1格式化程序「multipart/form-data」,「ExceptionType」:「System.InvalidOperationException」,「StackTrace」:「 ,IFormatterLogger formatterLogger個)\ r \ n在 System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider,HttpActionContext ActionContext中,的CancellationToken 的CancellationToken個)\ r \ n在 System.Web.Http.Controllers.HttpActionBinding。 <> C_ DisplayClass1.b _0(HttpParameterBinding parameterBinder個)\ r \ n在 System.Linq.Enumerable.WhereSelectArrayIterator 枚舉,的CancellationToken的CancellationToken)「}
任何線索如何解決這個?我們不希望做一個jQuery的職位,因爲我們必須允許後交叉領域。