在RazorMVC3中,使用ASP.NET视图引擎,我有一些结合了地址的文本框(如街道、街道nr、邮政编码)。
我想在我有足够的信息(如街道长度> 0,街道长度nr >0等)后调用Bing GeoCode the服务。如果在客户端验证所有的文本框都有足够的信息,然后进行回发(到Controller),这将是很好的。回发后,必须在视图中显示View服务调用的结果(经度/经度)。
如何做到这一点?
发布于 2012-02-17 20:23:53
型号:
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>
}https://stackoverflow.com/questions/9327790
复制相似问题