假设我有一堂课
public class ItemController:Controller
{
public ActionResult Login(int id)
{
return View("Hi", id);
}
}
Run Code Online (Sandbox Code Playgroud)
在不在Item文件夹中的页面上ItemController,我想创建一个指向该Login方法的链接.那么Html.ActionLink我应该使用哪种方法以及我应该传递哪些参数?
具体来说,我正在寻找替代方法
Html.ActionLink(article.Title,
new { controller = "Articles", action = "Details",
id = article.ArticleID })
Run Code Online (Sandbox Code Playgroud)
已经在最近的ASP.NET MVC化身中退役了.
我翻译了我的mvc网站,这个网站运行得很好.如果我选择其他语言(荷兰语或英语),内容将被翻译.这是有效的,因为我在会话中设置了文化.
现在我想在网址中显示所选文化(=文化).如果它是默认语言,则不应在网址中显示,只有当它不是默认语言时才应在网址中显示.
例如:
对于默认文化(荷兰语):
site.com/foo
site.com/foo/bar
site.com/foo/bar/5
Run Code Online (Sandbox Code Playgroud)
对于非默认文化(英语):
site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
Run Code Online (Sandbox Code Playgroud)
我的问题是我总是看到这个:
site.com/ nl/foo/bar/5即使我点击了英文(参见_Layout.cs).我的内容是用英文翻译的,但网址中的路由参数仍然是"nl"而不是"en".
我怎样才能解决这个问题或者我做错了什么?
我尝试在global.asax中设置RouteData但没有帮助.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Errors",
url: "Error/{action}/{code}",
defaults: new { controller = "Error", action = "Other", code = RouteParameter.Optional }
);
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "nl", controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" } …Run Code Online (Sandbox Code Playgroud) 我通过以下代码从资源中设置DisplayFormat中的NullDisplayText
public class LocalizedDisplayFormatAttribute : DisplayFormatAttribute
{
private readonly PropertyInfo _propertyInfo;
public LocalizedDisplayFormatAttribute(string resourceKey, Type resourceType)
: base()
{
this._propertyInfo = resourceType.GetProperty(resourceKey, BindingFlags.Static | BindingFlags.Public);
if (this._propertyInfo == null)
{
return;
}
base.NullDisplayText = (string)this._propertyInfo.GetValue(this._propertyInfo.DeclaringType, null);
}
public new string NullDisplayText
{
get
{
return base.NullDisplayText;
}
set
{
base.NullDisplayText = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的默认文化是"en-US",一旦我将文化更改为es-AR并加载页面工作正常,但是当我将文化更改回en-US时,字段不会被转换回来.
我通过以下方式改变文化
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
try
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("CurrentCulture");
string culutureCode = cookie != null && !string.IsNullOrEmpty(cookie.Value) ? cookie.Value …Run Code Online (Sandbox Code Playgroud) 更新07.01.2018
即使有人提出,这是一个jQuery问题而不是MVC问题,我认为这是一个MVC问题.我已经在asp.net core 2.0 MVC中创建了整个应用程序并且错误仍然存在.将它与MVC联系起来的是,我可以通过向[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]模型添加行来解决日期验证问题.因此,MVC对验证有影响.所以我会假设有在MVC某种方式来解决这个问题(见这个职位).请发布asp.net core 2.0的答案.
原帖
在MVC5页面中,我Double在文本框中渲染一个属性.加载页面时,该值显示为","作为小数分隔符,这是正确的,因为页面在德语系统上运行.如果我想保存表单,我会收到验证错误.怎么解决这个问题?我知道关于这个话题有一些问题,但据我所知,其中大部分已经过时了...我仍然在苦苦挣扎,没有设置或任何内置的东西允许来自不同国家的用户使用MVC应用程序.
模型:
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public Double Gewicht
{
get { return gewicht; }
set { gewicht = value; OnPropertyChanged(new PropertyChangedEventArgs("Gewicht")); }
}
Run Code Online (Sandbox Code Playgroud)
CSHTML:
<div class="form-group">
@Html.LabelFor(model => model.Gewicht, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-8">
@Html.EditorFor(model => model.Gewicht, new { htmlAttributes = new { @class = "form-control col-md-1" } })
@Html.ValidationMessageFor(model => model.Gewicht, "", …Run Code Online (Sandbox Code Playgroud) 我想用不同的语言创建一个网站.我已经读过我可以创建一个ActionFilter,但我有一个小问题:
我必须创建一个自定义的ModelBinder才能使用英语和德语数字格式(123,456,789.1vs. 123.456.789,1)
public class DecimalModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string key = bindingContext.ModelName;
var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
float outPut;
if (float.TryParse(v, NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out outPut))
return outPut;
return base.BindModel(controllerContext, bindingContext);
}
}
Run Code Online (Sandbox Code Playgroud)
此ModelBinder使用当前文化来决定使用哪种格式.但不幸的是,在ActionFilter改变文化之前就使用了ModelBinder.
如何在ModelBinder变为活动状态之前更改文化?
我有相当大的 Web 应用程序,它是用 ASP.NET MVC 5 和 MsSql 2008 开发的。在我的 PC 上,我有 +0700 UTC,但在我的共享主机上,我有其他时区。
这段代码给了我正确的日期时间。
DateTime utcTime = DateTime.UtcNow;
string zoneID = "N. Central Asia Standard Time";
TimeZoneInfo myZone = TimeZoneInfo.FindSystemTimeZoneById(zoneID);
DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone);
Console.WriteLine(custDateTime.ToString());
Run Code Online (Sandbox Code Playgroud)
不幸的是,我有很多地方可以处理日期和时间。我害怕,我忘记在任何地方更改代码。
有没有简单的方法可以为我的 Web 应用程序设置正确的时区?
PS 我的应用程序的所有用户都有相同的时区。
对于我想要支持的每种语言,我都有几个资源文件,命名如下:
NavigationMenu.en-US.resx
NavigationMenu.ru-RU.resx
NavigationMenu.uk-UA.resx
Run Code Online (Sandbox Code Playgroud)
文件位于MySolution/Resources/NavigationMenu文件夹中。
我有如下设置CurrentCulture和CurrentUICulture喜欢的动作
public ActionResult SetLanguage(string lang)
{
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
return Redirect(Request.UrlReferrer.AbsoluteUri);
}
catch(Exception)
{
return RedirectToAction("Index");
}
}
Run Code Online (Sandbox Code Playgroud)
lang参数值是uk-UA,ru-RU或者en-US取决于我的视图中的哪个链接被点击。我也有 web 配置全球化定义部分:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="ru-RU" uiCulture="ru-RU" />
Run Code Online (Sandbox Code Playgroud)
当我的应用程序启动时,我按预期使用俄语,但是当我尝试通过SetLanguage操作将语言更改为英语时,我的视图中没有语言更改。NavigationMenu.SomeProperty仍然是俄罗斯人。我错过了什么?
我正在为德国客户开发一个网站.在德国,他们使用逗号作为小数分隔符.
我使用WebMethod从SQL获取值,然后构建一个JSON对象以显示网站上的数据.使用C#SqlCommand,我将SQL中的值作为字符串获取.我想将此值保存为double变量.
这是代码示例,显示了我想要做的事情:
NumberFormatInfo nfi = new CultureInfo( "de-DE", false ).NumberFormat;
nfi.NumberDecimalSeparator = ","; // Displays the value with a comma as the separator.
string value ="15.95"; //value from SQL
string valueInString = "";
double valueInDouble = 0;
valueInString = Convert.ToDouble(value.ToString()).ToString( "N", nfi );
valueInDouble = Convert.ToDouble(value.ToString()).ToString( "N", nfi ); //Error
valueInDouble = Convert.ToDouble(valueInString,nfi);
Console.WriteLine( valueInString ); // returns 15,95. But it is a string
Console.WriteLine( valueInDouble ); // returns 15.95. the comma is reverted back to dot
Run Code Online (Sandbox Code Playgroud)
我需要将数据保存为double.我该如何解决这个问题?
c# ×7
asp.net-mvc ×6
.net ×3
actionlink ×1
asp.net-core ×1
html-helper ×1
jquery ×1
localization ×1
timezone ×1