在MVC3十进制错误 - 该值是无效现场 [英] error with decimal in mvc3 - the value is not valid for field
问题描述
[开始使用ASP.NET MVC 3] [1]。我不能与价格值= 9.99或9.99添加/编辑。它说:值9.99是无效的价格。和外地价格必须是一个数字。
如何解决这一问题?
型号:
公共类电影
{
公众诠释ID {搞定;组; }
公共字符串名称{搞定;组; }
公众的DateTime RELEASEDATE {搞定;组; }
公共字符串类型{搞定;组; }
公共十进制价格{搞定;组; }
}公共类MovieDbContext:的DbContext
{
公共DbSet<电影及GT;电影{搞定;组; }
}
控制器:
公共类MovieController:控制器
{
私人MovieDbContext DB =新MovieDbContext(); //
// GET:/电影/ 公众的ViewResult指数()
{
VAR电影=从db.Movies米
其中,m.ReleaseDate>新日期时间(1984年,6,1)
选择米; 返回查看(movie.ToList());
} //
// GET:/电影/详细资料/ 5 公众的ViewResult详细信息(INT ID)
{
电影电影= db.Movies.Find(ID);
返回查看(电影);
} //
// GET:/电影/制作 公众的ActionResult的Create()
{
返回查看();
} //
// POST:/电影/制作 [HttpPost]
公众的ActionResult创建(动画电影)
{
如果(ModelState.IsValid)
{
db.Movies.Add(电影);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 返回查看(电影);
} //
// GET:/电影/编辑/ 5 公众的ActionResult编辑(INT ID)
{
电影电影= db.Movies.Find(ID);
返回查看(电影);
} //
// POST:/电影/编辑/ 5 [HttpPost]
公众的ActionResult编辑(动画电影)
{
如果(ModelState.IsValid)
{
db.Entry(电影).STATE = EntityState.Modified;
db.SaveChanges();
返回RedirectToAction(「指数」);
}
返回查看(电影);
} //
// GET:/电影/删除/ 5 公众的ActionResult删除(INT ID)
{
电影电影= db.Movies.Find(ID);
返回查看(电影);
} //
// POST:/电影/删除/ 5 [HttpPost,ActionName(删除)]
公众的ActionResult DeleteConfirmed(INT ID)
{
电影电影= db.Movies.Find(ID);
db.Movies.Remove(电影);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 保护覆盖无效的Dispose(BOOL处置)
{
db.Dispose();
base.Dispose(处置);
}
}
}
查看:
@model MvcMovies.Models.Movie@ {
ViewBag.Title =创建;
}< H2>创建< / H><脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的> < / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>电影< /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Title)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Title)
@ Html.ValidationMessageFor(型号=> model.Title)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.ReleaseDate)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.ReleaseDate)
@ Html.ValidationMessageFor(型号=> model.ReleaseDate)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Genre)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Genre)
@ Html.ValidationMessageFor(型号=> model.Genre)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Price)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Price)
@ Html.ValidationMessageFor(型号=> model.Price)
< / DIV> &所述p为H.;
<输入类型=提交值=创建/>
&所述; / P>
< /字段集>
}< DIV>
@ Html.ActionLink(返回目录,索引)
< / DIV>
公共DbSet<电影及GT;电影{搞定;组; }
}
我刚刚在2年后偶然在此一次。我认为ASP.NET MVC 5已经解决了这一点,但看起来并非如此。因此,这里怎么去解决这个问题...
创建一个名为类 DecimalModelBinder
类似以下内容并把它添加到您的项目,例如根:
使用系统;
使用System.Globalization;
使用System.Web.Mvc;命名空间YourNamespace
{
公共类DecimalModelBinder:IModelBinder
{
公共对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName); ModelState中的ModelState =新的ModelState {值= valueResult}; 反对actualValue = NULL; 如果(valueResult.AttemptedValue!=的String.Empty)
{
尝试
{
actualValue = Convert.ToDecimal(valueResult.AttemptedValue,CultureInfo.CurrentCulture);
}
赶上(FormatException E)
{
modelState.Errors.Add(E);
}
} bindingContext.ModelState.Add(bindingContext.ModelName,ModelState中); 返回actualValue;
}
}
}
在的Global.asax.cs,
利用它在的Application_Start()
是这样的:
ModelBinders.Binders.Add(typeof运算(十进制),新DecimalModelBinder());
I'm following [Getting started with ASP.NET MVC 3][1]. And I can't add/edit with value of Price = 9.99 or 9,99. It said: "The value '9.99' is not valid for Price." and "The field Price must be a number."
How to fix this?
Model:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDbContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
Controller:
public class MovieController : Controller
{
private MovieDbContext db = new MovieDbContext();
//
// GET: /Movie/
public ViewResult Index()
{
var movie = from m in db.Movies
where m.ReleaseDate > new DateTime(1984, 6, 1)
select m;
return View(movie.ToList());
}
//
// GET: /Movie/Details/5
public ViewResult Details(int id)
{
Movie movie = db.Movies.Find(id);
return View(movie);
}
//
// GET: /Movie/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Movie/Create
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
//
// GET: /Movie/Edit/5
public ActionResult Edit(int id)
{
Movie movie = db.Movies.Find(id);
return View(movie);
}
//
// POST: /Movie/Edit/5
[HttpPost]
public ActionResult Edit(Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
//
// GET: /Movie/Delete/5
public ActionResult Delete(int id)
{
Movie movie = db.Movies.Find(id);
return View(movie);
}
//
// POST: /Movie/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
View:
@model MvcMovies.Models.Movie
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
public DbSet<Movie> Movies { get; set; }
}
I just stumbled on this again after 2 years. I thought ASP.NET MVC 5 had solved this but looks like it's not the case. So here goes how to solve the problem...
Create a class called DecimalModelBinder
like the following and add it to the root of your project for example:
using System;
using System.Globalization;
using System.Web.Mvc;
namespace YourNamespace
{
public class DecimalModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
ModelState modelState = new ModelState { Value = valueResult };
object actualValue = null;
if(valueResult.AttemptedValue != string.Empty)
{
try
{
actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture);
}
catch(FormatException e)
{
modelState.Errors.Add(e);
}
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
}
Inside Global.asax.cs,
make use of it in Application_Start()
like this:
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
这篇关于在MVC3十进制错误 - 该值是无效现场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!