【问题标题】:Form field values aren't binding to model when hosted on remote server jQuery Mobile MVC托管在远程服务器 jQuery Mobile MVC 上时,表单字段值未绑定到模型
【发布时间】:2013-02-08 05:38:39
【问题描述】:

我的 jQuery mobile \ MVC 应用程序有一个有点奇怪的问题,到目前为止我无法找到。

在我看来,我有 2 个 HTML.HiddenFor 字段。当表单发布时,这些字段中的值绑定到我的控制器中的模型类的属性。属性是 Double 类型。 在我的本地开发机器上发布表单时,值绑定正常。但是,当我的应用程序部署到远程服务器时,模型绑定停止工作,使其再次工作的唯一方法是将属性类型从 Double 更改为 string。

这是我的模型:-

 public class HomeViewModel
{
    public Double Latitude                  { get; set; }   //form value binds locally but does not bind on remote server
    public Double Longitude                 { get; set; }   //form value binds locally but does not bind on remote server
    public string LatitudeStr               { get; set; }   //form value binds 
    public string LongitudeStr              { get; set; }   //form value binds

这是我的看法:-

 @using (Html.BeginForm("Index","Branches", FormMethod.Post, new { ID = "frmSearch", data_transition = "none" })){       
   @Html.HiddenFor(model => model.Latitude, new { id = "hdnLat" })  
   @Html.HiddenFor(model => model.Longitude, new { id = "hdnLong" })  

这是我的控制器:-

  [HttpPost]
    public ActionResult Index(HomeViewModel model)
    {

当我在提交表单后检查页面时,我可以看到 input-validation-error 类已附加到隐藏元素:

查看 Firebug 中的发布信息,我可以看到已发布值。

以上在本地运行良好,但不能从远程服务器运行。

据我所知,在本地和远程运行时,完全相同的值会回传到服务器

如果有人能对此有所了解,那就太好了

【问题讨论】:

  • 哪个部分不工作?你能在你的索引中打断点吗?你得到 404 了吗?
  • 不起作用的部分是控制器中字段值与模型的绑定。我可以在萤火虫中看到已发布值,但在控制器中 model.Latitude 和 model.longitude 值为空。这只发生在远程运行应用程序时,它工作正常并且值在本地绑定良好
  • 当你说远程服务器 - 你正在使用同一个客户端发布但应用程序不再在同一个盒子上运行?
  • 是的,我使用的是同一个客户端,但应用程序部署到本地网络之外的 Web 服务器

标签: c# asp.net-mvc jquery-mobile asp.net-mvc-4


【解决方案1】:

为您的隐藏字段添加验证器。您可能会收到您不知道的验证消息,或者将验证摘要设置为显示模型错误。在服务器上的文化与使用逗号作为分隔符的客户端发生冲突之前,我在浮动方面遇到了类似的问题,这可能就是为什么它作为字符串工作的原因,因为它在解析 Double 时不再失败。

【讨论】:

  • 这很准确。服务器上的区域和语言设置为瑞典语,因此 lat\long 字符串值不再转换为 Double,MVC 模型绑定只是忽略了这些值。当调用 Convert.ToDouble() 方法并传入 lat/long 值时,我得到一个“输入字符串的格式不正确”错误。在 IIS7.5 中将站点上的 .NET 全球化中的 Culture \ UI Culture 设置为英语(英国)(en-GB),然后在将值发布到服务器之前调用 parseFloat 并在 javascript 中传入 lat\long 字符串为我修好了
猜你喜欢
  • 1970-01-01
  • 2019-01-03
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
相关资源
最近更新 更多