【发布时间】:2011-07-10 18:37:42
【问题描述】:
这是我在这里提出的另一个问题:Find nearby entities with Bing Maps REST control
在上面的问题中,在函数 DisplayResults(response) 的 for 循环中,我添加了以下代码:
var loc = new Array();
loc[0] = results[i].Longitude;
loc[1] = results[i].Latitude;
var address = {
AddressLine1: results[i].Address,
City: results[i].City,
State: results[i].StateOrProvince,
PostalCode: results[i].PostalCode,
Latitude: results[i].Latitude,
Longitude: results[i].Longitude,
Country: results[i].CountryOrRegion,
ID: results[i].UniqueId
};
$.ajax({
url: "/Home/AddAddressToCollection",
type: 'post',
data: JSON.stringify(address),
contentType: 'application/json',
dataType: 'json'
});
上面的代码从结果数据集中包含的每个地址构造一个地址,并将其发送回 ContentResult,该结果返回 null,因为它的目的是将每个地址存储到后端,即 MongoDB。您可能想知道我为什么采用这种方法 - 地址是 MongoDB 的种子数据。这个想法是,这是一个开源项目,我想添加一些个性化来使用开发人员的 GeoLocation,方法是为他们提供一组与他们检测到的位置相对应的本地化地址(该代码都在我之前的帖子中)。
这是 HomeController 中 ContentResult 的代码:
[HttpPost]
public ContentResult AddAddressToCollection(Address address)
{
address.GeoInsert(_dbName, ref _repository);
return null;
}
当我的 Address 类如下所示时,此代码可以与相应的 POST 方法一起使用:
公开课地址 { 私人双纬度; 私人双 _longitude;
[Key]
[Required]
[HiddenInput(DisplayValue=false)]
public string ID { get; set; }
[Required]
[HiddenInput(DisplayValue = false)]
public virtual Guid AccountID { get; set; }
[Required]
[Display(Name = "Address Line 1")]
public string AddressLine1 { get; set; }
[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "State")]
public string State { get; set; }
[Required]
[Display(Name = "Zip Code")]
public string PostalCode { get; set; }
public string Country { get; set; }
[Required]
[HiddenInput(DisplayValue = false)]
public double[] Location { get; set; }
[Required]
[HiddenInput(DisplayValue = false)]
public double Latitude
{
get { return _latitude; }
set
{
Location[1] = value;
_latitude = value;
}
}
[Required]
[HiddenInput(DisplayValue = false)]
public double Longitude
{
get { return _longitude; }
set
{
Location[0] = value;
_longitude = value;
}
}
public Address()
{
Location = new double[2];
}
public void GeoInsert(string dbName, ref Repository repository)
{
repository.MongoGeoInsert(dbName, this);
}
public Dictionary<Address, double> GeoNear(string dbName, ref Repository repository, double maxKilometers, int limitResults)
{
return repository.MongoGeoNear<Address>(dbName, this.Latitude, this.Longitude, maxKilometers, limitResults);
}
}
但这就是我好奇的地方......虽然我现在不需要这个,但我最初尝试让我的模型中的所有类都从同一个基类继承,以帮助我使用一些自定义序列化方法.当我从这样一个类继承......让我们称之为 BaseClass,POST 不再工作,我从服务器收到 500 响应......我只是不清楚原因是什么。这是我正在谈论的一个示例(p.s.如果您碰巧使用以下任何代码,只是提示您应该弄清楚如何使用 Dictionary 进行序列化......它要快得多,但人们更熟悉有反射,所以这就是我要在这里做的):
这么快,对于 Address 类,我将拥有:
地址:基类
构造函数是……
地址 : base("地址") { //尽管 base("Address") 不是必需的,除非你使用字典 //序列化方法 }
现在是 BaseClass...
public class Serialization
{
public abstract class BaseObject
{
[BsonId(IdGenerator = typeof(CombGuidGenerator))]
public Guid? Id { get; set; }
public string ObType { get; private set; }
public BaseObject() : this("base") { }
public BaseObject(string obType)
{
ObType = obType;
Id = Guid.NewGuid();
}
public virtual BaseObject Parse(XElement xml)
{
PropertyInfo[] properties = this.GetType().GetProperties();
foreach (var property in properties)
{
ElementName attribute = Attribute.GetCustomAttribute(property, typeof(ElementName)) as ElementName;
if (attribute == null) continue;
object value = null;
string stringValue = xml.Element(attribute.Name).Value;
switch (property.PropertyType.Name.ToLower())
{
case "double":
{
value = double.Parse(stringValue);
break;
}
default:
{
value = stringValue;
break;
}
}
property.SetValue(this, value, null);
}
return this;
}
}
public class Serializer
{
public static T Deserialize<T>(XElement xml)
where T : BaseObject, new()
{
BaseObject obj = new T() as BaseObject;
if (obj != null)
{
//populate obj properties...
obj.Parse(xml);
}
return obj as T;
}
}
}
好的,我认为这应该是足够的信息......有什么想法不起作用?希望我在这里没有遗漏一些非常明显的东西......但我做的 Silverlight 比 ASP.NET 编程要多得多,所以我完全有可能是 0:)
谢谢!
【问题讨论】:
-
你能打开 firebug 告诉我们你遇到了什么错误吗??
-
是的,这是一个非常标准的 500 错误:POST /Home/AddAddressToCollection 500 Internal Server Error 我认为它与 MVC 框架如何处理内容和操作结果有关,因为我知道代码客户端正在工作(因为当我不从 BaseClass 继承时它工作)
标签: jquery ajax asp.net-mvc-3 mongodb geolocation