Placing a marker on google maps using asp.net-core razor pages

[亡魂溺海] 提交于 2021-02-11 12:35:43

问题


I have the following 3 pages in my project:

Create.cshtml:

@page
@model AdminPortal.Web.Pages.Assets.CreateModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Asset</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Asset.ID" class="control-label"></label>
                <input asp-for="Asset.ID" class="form-control" />
                <span asp-validation-for="Asset.ID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Asset.ZIP" class="control-label"></label>
                <input asp-for="Asset.ZIP" class="form-control" />
                <span asp-validation-for="Asset.ZIP" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input class="btn btn-primary" onclick="setMarker(@Model.Asset.ZIP)" type="submit" value="Create" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Create.cshtml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using AdminPortal.Web.Data;
using AdminPortal.Web.Models;
using Microsoft.JSInterop;

namespace AdminPortal.Web.Pages.Assets
{
    public class CreateModel : PageModel
    {
        private readonly AssetDbContext _context;

        public CreateModel(AssetDbContext context, Asset asset)
        {
            _context = context;
            Asset = asset;
            Asset.ZIP = "411038";
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public Asset Asset { get; set; }

        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }


            _context.Assets.Add(Asset);
            await _context.SaveChangesAsync();
            
            return RedirectToPage("./Index");
        }
    }
}

setMarker function in a file called MapConf.js:

function setMarker(zipcode) {
    getLatLngByZipcode(zipcode, function (latLng) {
        let marker = new google.maps.Marker({
            position: latLng,
            title: "This is an installation"
        });
        map.setMarker(latLng);
        map.setCenter(latLng);
        map.setZoom(16);
    });
}

The codes are supposed to place a marker at the postal-code entered by the user.


However, the code is throwing NullReferenceException (which I suspect is because the Asset.ZIP is not really "set" until the OnPostAsync() method is called). How would I achieve this?

来源:https://stackoverflow.com/questions/65904135/placing-a-marker-on-google-maps-using-asp-net-core-razor-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!