带有小数的奇怪 Model Binder 行为
我对奇怪的模型绑定器行为感到困惑:
- 当我使用包含小数参数的 RedirectToAction 时(因为在俄语中,我们使用“,”而不是“.”,例如 1,5; 2,5),在查看它时将其与点绑定,“4,5 => ; 4.5",在发布表单后,我出现 ModelState 错误,并且值等于 0。但是,当输入整数时,一切都会按预期进行。
我该如何修复它?
型号:
public class TestA
{
public double FirstNumberA { get; set; }
public double SecondNumberA { get; set; }
}
public class TestB
{
public double FirstNumberB { get; set; }
public double SecondNumberB { get; set; }
}
控制器:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(TestA model)
{
return RedirectToAction("About", new { firstNumberB = model.FirstNumberA, secondNumberB = model.SecondNumberA });
}
public ActionResult About(double firstNumberB, double secondNumberB)
{
return View(new TestB() { FirstNumberB = firstNumberB, SecondNumberB = secondNumberB });
}
[HttpPost]
public ActionResult About(TestB model)
{
return View();
}
视图: /* 索引 */
@model TestA
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.FirstNumberA)
@Html.TextBoxFor(x => x.SecondNumberA)
<input type="submit" name="submit" value="submit" />
}
/* 关于 */
@model TestB
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.FirstNumberB)
@Html.TextBoxFor(x => x.SecondNumberB)
<input type="submit" name="submit" value="submit" />
}
更新 当删除 About.cshtml 中的 TextBoxFor(x => x.SecondNumberB) 并发布表单时 - FirstNumberB 将导致 ModelState 错误,而 SecondNumberB 按应有方式绑定。
更新2 我认为,这是另一个问题,在他的 post - asp.mvc 首先在查询字符串中查找值并获取它。在我的例子中,在查询字符串中,我有 ?firstNumber=1.5 ,它“按原样”绑定到 TextBox,没有区域性规则。发布后我们收到 FormatException 错误。如何修复它 - 使用正确的区域性小数分隔符将十进制值绑定到文本框?我无法使用任何 JavaScript。我考虑在 ReturnToRedirect 之前使用 TempData 解决方法,但在这种情况下用户无法重新加载页面。
I am confused by strange model binder behavior:
- when I use RedirectToAction with parameters which includes fractional number (because in Russian, we use "," instead of ".", e.x. 1,5; 2,5) in View it Binds that with dots, "4,5 => 4.5", and after post form I have ModelState error and values equal to 0. When inputting integer numbers however, all works as expected.
How i can fix it?
Models:
public class TestA
{
public double FirstNumberA { get; set; }
public double SecondNumberA { get; set; }
}
public class TestB
{
public double FirstNumberB { get; set; }
public double SecondNumberB { get; set; }
}
controller:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(TestA model)
{
return RedirectToAction("About", new { firstNumberB = model.FirstNumberA, secondNumberB = model.SecondNumberA });
}
public ActionResult About(double firstNumberB, double secondNumberB)
{
return View(new TestB() { FirstNumberB = firstNumberB, SecondNumberB = secondNumberB });
}
[HttpPost]
public ActionResult About(TestB model)
{
return View();
}
views:
/* index */
@model TestA
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.FirstNumberA)
@Html.TextBoxFor(x => x.SecondNumberA)
<input type="submit" name="submit" value="submit" />
}
/* about */
@model TestB
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.FirstNumberB)
@Html.TextBoxFor(x => x.SecondNumberB)
<input type="submit" name="submit" value="submit" />
}
Upd
When remove TextBoxFor(x => x.SecondNumberB) in About.cshtml and post form - FirstNumberB will cause an ModelState error, when SecondNumberB binds as should.
Upd2
I think, that here is another problem, that describe Phil Haack in his post - asp.mvc first look for values at the query string and get it. And in my case in query string I have ?firstNumber=1.5 and it binds to TextBox "as-is", without culture rules. And after post we get FormatException error. How can I fix it - bind decimal values to textboxes with correct culture decimal-separators? I can't use any JavaScript. I think about workaround with TempData before ReturnToRedirect, but in this case user can't reload the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是文化问题。确保在 web.config
元素中设置正确的区域性。Could be a culture problem. Make sure you set the correct culture in your web.config
<globalization>
element.