簡體   English   中英

System.Data.Entity.Spatial.DbGeography-沒有無參數構造函數

[英]System.Data.Entity.Spatial.DbGeography - No Parameterless Constructor

我有一個提交頁面,其中表單字段之一是緯度/經度坐標。 我將它們存儲在名為“ Location”的System.Data.Entity.Spatial.DbGeography中,並渲染如下字段:

<div class="form-group">
            @Html.LabelFor(model => model.Location.Longitude, new { @class = "control-label col-md-2" })
            <div class="col-sm-3">
                @Html.TextBoxFor(model => model.Location.Longitude)
                <img class="load-icon" src="~/Content/Loading.gif" />
                @Html.ValidationMessageFor(model => model.Location.Longitude)
            </div>

            @Html.LabelFor(model => model.Location.Latitude, new { @class = "control-label col-md-2" })
            <div class="col-sm-3">
                @Html.TextBoxFor(model => model.Location.Latitude)
                <img class="load-icon" src="~/Content/Loading.gif" />
                @Html.ValidationMessageFor(model => model.Location.Latitude)
            </div>
        </div>

在控制器中,我收到另一面的表格:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "...Location...")] Report report)

但是,每當我使用表單中的值進行POST時,它都會引發一個異常,指出某個對象(它無法告訴我哪個對象)沒有參數少的構造函數。 當我從綁定字符串中刪除“ Location”但保留所有其他屬性(在我的示例中省略)時,它可以正常工作。

這似乎表明問題出在,它試圖創建一個DbGeography放入“報告”中,但是失敗了,因為它無法實例化它。 這個對嗎? 如果是這樣,如何避免此錯誤? 我寧願不在數據庫中使用自定義類,但也無法想到其他任何選擇。

最好的方法是使用sqlQueryCommand,此代碼100%有效:

    var id = form["id"];
            var name = form["name"];
            var lat = form["Location.Latitude"];
            var lng = form["Location.Longitude"];
            db.Database.ExecuteSqlCommand("insert into [dbo].[schoolinfo] values ('" + id + "','" + name + "',geography::Point(" + lat + ", " + lng + ", 4326))");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM