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