Tag: asp.net mvc 4

使用Web API和JSON.NET序列化对象时,防止$ id / $ ref

在序列化对象时,我似乎无法阻止Web API / JSON.NET使用Newtonsoft.Json.PreserveReferencesHandling.Objects 。 换句话说,尽管使用了以下设置,但$ id / $ ref总是在序列化对象中使用: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start () { WebApiConfig.Register(GlobalConfiguration.Configuration); } } public static class WebApiConfig { public static void Register (HttpConfiguration config) { JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType().Single(); jsonFormatter.UseDataContractJsonSerializer = false; jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None; } } 有任何想法吗?

了解MVC中的,和Complex Actionmethod参数

我对MVC的设计模式和框架都非常陌生。 我也不是非常精通ASP.NET Forms的基础知识。 但是,我也了解Web开发和HTTP Post和GET的基础知识。 现在,我已经阅读了一些MVC教程,虽然我很好地掌握了模式的MVC以及“路由引擎”的工作原理。 然后突然我遇到了一个看起来像下面的代码: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { return Content(“Thanks”, “text/html”); } } 我很少看到它的问题: 我对路由引擎的理解是控件被传递给基于URL 的特定 ActionMethod,而且通常URL基本上是Controller / ActionMethod / Id类型,其中参数to action menthod是Primitive类型。 在上面的这个例子中,调用“需要什么样的URL” 公共ActionResult索引(MyViewModel模型)?“ 由于NyViewModel是一种复杂类型,因此您无法将其作为URL的一部分传递。 你怎么称呼它? 当第一种方法不需要任何属性时,为什么第二种方法用[HttpPost]装饰? 是否有关于何时使用[Http]属性的指南,何时没有? 我想我在拼图中错过了一个很大的问题,两个问题都是相互关联的。 但是,需要一些帮助才能理解这种关系

MVC中的属性和filter有什么区别

现在,我可以请一个比较,而不仅仅是一个定义。 例: SomeClassAttribute (or ISomeClassAttribute) VS SomeClassFilter (or ISomeClassFilter) 我觉得它们可以以相同的方式使用,但一般来说“应用属性”和“filter是它们产生的function”。 所以我可以“为方法(或类或其他)添加属性以应用filter。

EF5,SQL Server,经度和纬度

我发现在SQL Server中存储lat和long的最佳类型是decimal(9,6)(ref。 在SQL数据库中存储纬度和经度数据时使用什么数据类型? )所以我做了 AddColumn(“dbo.Table”, “Latitude”, c => c.Decimal(nullable: false, precision: 9, scale: 6)); AddColumn(“dbo.Table”, “Longitude”, c => c.Decimal(nullable: false, precision: 9, scale: 6)); SQL似乎没问题,一切正常,但是当我插入/更新一个值时,即 lat = 44.5912853 它像这样保存: 44.590000 我检查了流程,就在更新之前,我的实体包含正确的值,所以我认为不是与我的代码相关,而是与EF / SQL的一些相关。 你有什么想法可以避免这种情况吗? UPDATE update [dbo].[Breweries] set [RankId] = @0, [Name] = @1, [Foundation] = null, [DirectSale] = @2, [OnlineSale] = @3, [StreetAddress] = […]

ASP.NET MVC 4中未翻译自定义的错误消息

我想翻译validation消息“字段日期必须是日期”。 我已将以下键添加到Global.asax的Application_Start()中 ClientDataTypeModelValidatorProvider.ResourceClassKey = “ModelBinders”; DefaultModelBinder.ResourceClassKey = “ModelBinders”; 我在App_GlobalResources中创建了ModelBinders.resx,ModelBinders.nl.resx,ModelBinders.fr.resx。 我在.resx文件中添加了以下字符串资源(或翻译): Name Value ==== ===== FieldMustBeDate The field {0} must be a date. FieldMustBeNumeric The field {0} must be a number. PropertyValueInvalid The value ‘{0}’ is not valid for {1}. PropertyValueRequired A value is required. 当我提交日期字符串时,我将收到“FieldMustBeDate”的翻译。 当我提交无效日期(例如“01/01 / 201a”)时,我收到默认ModelBinders.resx中定义的“PropertyValueInvalid”的未翻译消息,而不是翻译…如何显示正确的翻译对于PropertyValueInvalid?

TextBoxFor值在post后没有更新

我有一个简单的强类型视图,但我似乎无法在post后更新我的表单上的文本框。 这是我的模型: public class Repair { public string Number { get; set; } } 在我看来是一个TextBox: @Html.TextBoxFor(x => x.Number) 我试图在post到我的控制器后更新文本框: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(Repair r) { r.Number = “New Value”; return View(r); } 即使我将Number设置为新值,文本框中的文本也不会更改。 我究竟做错了什么?

在尝试标记odata时,可查询属性未在mvc 4 web api中进行编译

我正在使用visual studio 2012附带的MVC 4 web api。 据我所知,odata不会自动解析,但我们需要在行动中添加[Queryable]属性。 所以我添加了它: public class TestController : ApiController { [Queryable] public IQueryable GetMyClasses() { return …; } } 但我得到一个编译错误: The type or namespace name ‘Queryable’ could not be found (are you missing a using directive or an assembly reference?) odata仍然受支持吗? 以及为什么Queryable不被识别为此处列出的属性。 谢谢

如何从本地存储的文件中读取JSON?

我试图使用JSON.Net加载本地存储在ASP.Net MVC 4站点上的JSON文件,但是在指向该文件时遇到问题。 这是我想要做的: List treatments = JsonConvert.DeserializeObject<List>(Server.MapPath(“~/Content/treatments.json”)); 并且我遇到了这个错误: An exception of type ‘Newtonsoft.Json.JsonReaderException’ occurred in Newtonsoft.Json.dll but was not handled in user code Additional information: Unexpected character encountered while parsing value: c. Path ”, line 0, position 0. 我应该做些什么不同的事情?

不引人注意的DateTime? 在MVC4中validation

我将MVC3解决方案升级为MVC4。 迁移后,validation程序已损坏。 如果我选择德语作为语言,我的输入日期是“20.03.2013”​​。 我在MVC4中得到validation错误,但在MVC3中没有。 如果我将格式从“20.03.2013”​​替换为“20/03/2013”​​,它适用于MVC4,但不适用于MVC3 😉 我将当前线程的UI文化设置为德语。 ResX值的输出是正确的语言, 所以我知道文化不应该有错误。 ,仅适用于网站本身。 错误消息是英文的,但该网站是德语。 我认为这意味着validation器使用了错误的UI文化。 这是我使用的代码。 [必需(ErrorMessageResourceName =“Error_DepartureDate”,ErrorMessageResourceType = typeof(Resx.Query))] 公共日期时间? DepartureDate {get; 组; } 我假设默认模型绑定器有问题,因为渲染的html看起来很好: data-lang =“de”data-mindate =“3”data-val =“true”data-val-required =“Bitte geben Sie das gew&#252; nschte Reisedatum des Hinflugs ein。” id =“DepartureDate”name =“DepartureDate”tabindex =“3”type =“text”value =“” 我将Jscript升级到使用Visual Studio 2012(已安装SP1)模板创建新Mvc应用程序时发布的源代码。 这没有任何影响。 我有一个CultureModelBinder,它从Session中读取当前的文化,并使用一个小帮助函数设置文化。 public static void UpdateThreadCulture(CultureInfo culture) { Thread.CurrentThread.CurrentUICulture […]

如何处理System.Data.Entity.Validation.DbEntityValidationException?

我的应用程序收到以下错误: EntityFramework.dll中出现“System.Data.Entity.Validation.DbEntityValidationException”类型的exception,但未在用户代码中处理 附加信息:一个或多个实体的validation失败。 有关详细信息,请参阅“EntityValidationErrors”属性。 尝试注册新用户时出现此错误。 ‘db.SaveChanges()’发生错误 这是代码: public ActionResult Registration(x.Models.User user) { if(ModelState.IsValid) { using(var db = new xDBEntities1()) { var crypto = new SimpleCrypto.PBKDF2(); var encrpPass = crypto.Compute(user.password); var sysUser = db.users.Create(); sysUser.email = user.email; sysUser.username = user.username; sysUser.password = encrpPass; sysUser.premium_credits = 0; sysUser.login_times = 0; sysUser.last_ip = Request.ServerVariables[“REMOTE_ADDR”]; sysUser.creation_ip = Request.ServerVariables[“REMOTE_ADDR”]; sysUser.banned […]