将 html 数据类型发布到 webapi post 方法时遇到一些问题。
这是邮政编码
$('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";
这就是模型
公共类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":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'LeadModel' from content with media type'multipart/form-data '.","ExceptionType":"System.InvalidOperationException","StackTrace":" at 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 格式化程序,IFormatterLogger formatterLogger)\r\n在 System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancelToken)\r\n 在 System.Web.Http.Controllers.HttpActionBinding.<>c_ DisplayClass1.b_0(HttpParameterBinding parameterBinder)\r\n at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()\r\n at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator
1 枚举器,CancellationToken cancelToken)"}
任何线索如何解决这个问题?我们不想发布 jquery 帖子,因为我们必须允许发布跨域。