在MVC3中使用Razor替换图像链接的最佳方法是什么.我现在只是这样做:
<a href="@Url.Action("Edit", new { id=MyId })"><img src="../../Content/Images/Image.bmp", alt="Edit" /></a>
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我在项目中添加了一个cshtml页面.当我尝试向其添加以下声明时,我收到一个错误:"当前上下文中不存在名称'model'".
@model xyz.abc.SomeClass
Run Code Online (Sandbox Code Playgroud)
我检查了参考文献,所有这些都已到位.我在视图文件夹中添加了一个web.config,但是没有修复它.
有什么我想念的吗?
如果我在我的模型类中有一个类型的属性,DateTime我该如何以特定的格式呈现它 - 例如以ToLongDateString()返回的格式?
我试过这个......
@Html.DisplayFor(modelItem => item.MyDateTime.ToLongDateString())
Run Code Online (Sandbox Code Playgroud)
...抛出异常,因为表达式必须指向属性或字段.还有这个...
@{var val = item.MyDateTime.ToLongDateString();
Html.DisplayFor(modelItem => val);
}
Run Code Online (Sandbox Code Playgroud)
...它不会抛出异常,但渲染的输出为空(尽管val包含预期值,正如我在调试器中看到的那样).
感谢提前提示!
编辑
ToLongDateString只是一个例子.我真的想使用,而不是ToLongDateString为一个自定义的扩展方法DateTime和DateTime?:
public static string FormatDateTimeHideMidNight(this DateTime dateTime)
{
if (dateTime.TimeOfDay == TimeSpan.Zero)
return dateTime.ToString("d");
else
return dateTime.ToString("g");
}
public static string FormatDateTimeHideMidNight(this DateTime? dateTime)
{
if (dateTime.HasValue)
return dateTime.Value.FormatDateTimeHideMidNight();
else
return "";
}
Run Code Online (Sandbox Code Playgroud)
所以,我认为我不能在ViewModel属性上使用DisplayFormat属性和DataFormatString参数.
我使用VS 2015 RC和MVC模板创建了一个新的应用程序,并且没有修改任何代码行我有这个错误:
Method not found: '!!0[] System.Array.Empty()'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: Method not found: '!!0[] System.Array.Empty()'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. …Run Code Online (Sandbox Code Playgroud) 在我的MVC应用程序中,我使用以下代码上传文件.
模型
public HttpPostedFileBase File { get; set; }
Run Code Online (Sandbox Code Playgroud)
视图
@Html.TextBoxFor(m => m.File, new { type = "file" })
Run Code Online (Sandbox Code Playgroud)
一切工作正常..但我试图将结果fiel转换为byte [].我怎么能这样做
CONTROLLER
public ActionResult ManagePhotos(ManagePhotos model)
{
if (ModelState.IsValid)
{
byte[] image = model.File; //Its not working .How can convert this to byte array
}
}
Run Code Online (Sandbox Code Playgroud) 当jquery ajax调用一个动作时,如何处理控制器中抛出的异常?
例如,我想在ajax调用期间在任何类型的服务器异常上执行全局javascript代码,如果处于调试模式或仅显示正常错误消息,则会显示异常消息.
在客户端,我将调用ajax错误的函数.
在服务器端,我是否需要编写自定义actionfilter?
如何在mvc视图中编写注释,不会传输到最终的html(即浏览器,响应).人们可以发表评论
<!--<a href="/">My comment</a> -->
Run Code Online (Sandbox Code Playgroud)
但它在浏览器的页面源代码中可见.
是否可以在cshtml文件中留下评论仅供内部使用?
如何使用Razor视图引擎在ASP.NET MVC3中创建只读文本框?是否有HTMLHelper方法可以做到这一点?
像这样的东西?
__CODE__
我有两个控制器,都叫AccountController.其中一个,让我们调用它Controller A,在一个Area被调用Admin,另一个,让我们调用它Controller B,不在任何Area(我想这意味着它是在默认情况下Area?). Controller B有一个action method叫Login.我有一个action method在Controller A,它有这条线
return RedirectToAction("LogIn", "Account");
Run Code Online (Sandbox Code Playgroud)
问题是,404当这一行被执行时,我得到一个因为尝试重定向到不存在action的行Controller A.我想打电话给action method在Controller B.这可能吗?
我有一个遗留代码问题,需要我支持随机网址,就好像它们是对主页的请求一样.某些URL中包含的字符会生成错误"从客户端(&)检测到潜在危险的Request.Path值".该站点使用ASP.Net MVC 3(在C#中)编写,并在IIS 7.5上运行.
这是一个示例URL ...
http://mywebsite.com/Test123/This_&_That
Run Code Online (Sandbox Code Playgroud)
这是我如何设置我的全部路线(我有其他路线来捕捉特定页面)......
routes.MapRoute(
"Default", // Route name
"{garb1}/{garb2}", // URL with parameters
new { controller = "Website", action = "Home", garb1 = UrlParameter.Optional, garb2 = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
我在web.config文件中添加了以下内容...
<configuration>
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
<configuration>
Run Code Online (Sandbox Code Playgroud)
我还在应该捕获URL的操作中添加了ValidateInput属性...
public class WebsiteController : Controller
{
[ValidateInput(false)]
public ActionResult Home()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到错误.有什么想法吗?我错过了什么?现在我只是在我的本地开发服务器上运行(我还没有在生产中尝试过这些修复).
asp.net-mvc-3 ×10
asp.net-mvc ×5
razor ×4
c# ×3
asp.net ×2
image ×2
action ×1
arrays ×1
c#-4.0 ×1
comments ×1
html-helper ×1
jquery ×1