简体   繁体   中英

System.Data.Entity.Spatial.DbGeography - No Parameterless Constructor

I have a submission page where one of the form's fields are lat/long coordinates. I store them in a System.Data.Entity.Spatial.DbGeography called 'Location', and render the fields like this:

<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>

In my controller, I receive the form on the other side:

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

But every time I POST with the values from the form it throws an exception, stating that some object (it fails to tell me which) does not have a parameter less constructor. When I remove 'Location' from the bind string but keep all of the other properties (omitted in my sample), it works fine.

That seems to indicate that the issue is that it's trying to create a DbGeography to put in to 'report', but failing because it can't instantiate it. Is this correct? If so, how can I avoid this error? I would rather not use a custom class in my database, but I can't think of any other options.

the best way it's to use a sqlQueryCommand this the code 100% worked :

    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))");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM