모델 :
public class GeoCodeViewModel
{
[StringLength(70, MinimumLength = 1)]
public string Street { get; set; }
[StringLength(10, MinimumLength = 1)]
public string PostCode { get; set; }
}
컨트롤러 :
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new GeoCodeViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(GeoCodeViewModel model)
{
if (!ModelState.IsValid)
{
// the model is not valid => redisplay the view so that
// the user can fix his errors
return View(model);
}
// TODO: at this stage the model is valid => call the web service
...
}
}
보기 :
@model GeoCodeViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.Street)
@Html.EditorFor(x => x.Street)
@Html.ValidationMessageFor(x => x.Street)
</div>
<div>
@Html.LabelFor(x => x.PostCode)
@Html.EditorFor(x => x.PostCode)
@Html.ValidationMessageFor(x => x.PostCode)
</div>
<p><button type="submit">OK</button></p>
}
들으 대린. 그러나 웹 서비스에 대한 호출은 포스트 백 (postback)이 아닌 클라이언트 시스템에서 웹 서비스에 대한 호출을 여전히 원할 때 포스트 백 (postback)으로 수행됩니다. 그래서 요약 :보기가 편집 가능한 상태에 있어야하고 종료시 텍스트 상자가있는 동안 최소한의 정보 만 있으면 웹 서비스를 호출해야합니다. 그게 가능하니? –
@PatrickPeters, 어떻게 자바 스크립트에서 다른 도메인에있는 웹 서비스를 호출 할 계획입니까? Bing API가 JSONP를 지원합니까? –
webservice는 기본 HTTP (http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc)입니다. 주소를 기반으로 위도/경도를 계산하는 루틴은 별도의 C# 클래스 (WS 호출의 복잡성을 숨 깁니다)에 래핑되었습니다. –