2012-02-17 2 views
0

면도기 뷰 엔진을 사용하는 ASP.NET MVC 3에서 주소 (예 : 거리, 거리 번호, 우편 번호)를 결합하는 몇 개의 텍스트 상자가 있습니다.면도기 텍스트 상자에 연결된 지오 코드 계산을 수행하는 방법은 무엇입니까?

통화 정보가 충분한 경우 (예 : 거리가 0보다 길고 길이가 0보다 큰 경우 등) Bing GeoCode webservice를 호출하려고합니다. 모든 텍스트 상자에 충분한 정보가 있으면 클라이언트 사이트에서 유효성을 검사 한 다음 컨트롤러에 다시 게시하는 것이 좋습니다. 다시 게시 후에 웹 서비스 호출의 결과 (위도/경도)가보기에 표시되어야합니다.

어떻게이 작업을 수행 할 수 있습니까?

답변

0

모델 :

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> 
} 
+0

들으 대린. 그러나 웹 서비스에 대한 호출은 포스트 백 (postback)이 아닌 클라이언트 시스템에서 웹 서비스에 대한 호출을 여전히 원할 때 포스트 백 (postback)으로 수행됩니다. 그래서 요약 :보기가 편집 가능한 상태에 있어야하고 종료시 텍스트 상자가있는 동안 최소한의 정보 만 있으면 웹 서비스를 호출해야합니다. 그게 가능하니? –

+0

@PatrickPeters, 어떻게 자바 스크립트에서 다른 도메인에있는 웹 서비스를 호출 할 계획입니까? Bing API가 JSONP를 지원합니까? –

+0

webservice는 기본 HTTP (http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc)입니다. 주소를 기반으로 위도/경도를 계산하는 루틴은 별도의 C# 클래스 (WS 호출의 복잡성을 숨 깁니다)에 래핑되었습니다. –

관련 문제