razor

Nesting TagHelpers in ASP.NET Core MVC

*爱你&永不变心* 提交于 2019-12-17 16:35:24
问题 The ASP.NET Core TagHelper documentation gives the following example: public class WebsiteContext { public Version Version { get; set; } public int CopyrightYear { get; set; } public bool Approved { get; set; } public int TagsToShow { get; set; } } [TargetElement("website-information")] public class WebsiteInformationTagHelper : TagHelper { public WebsiteContext Info { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "section";

Open the href mailto link in new tab / window

瘦欲@ 提交于 2019-12-17 16:19:23
问题 I have an image which when click, I want to link to a mailto: <a id="mailto" href="mailto:hfms@live.com.my" target="_newtab" > <img src="@Url.Content("~/Content/HomePage/email.png")" alt="email" /></a> However, currently once its clicked, it will launch the email option to choose a mailto application, and once i choose, the mailto link is open in the current tab. This will cause user to leave the application. So, I want the page to sent email (by gmail, yahoo, etc ) is either open in new tab

displaying image from db in Razor/MVC3

僤鯓⒐⒋嵵緔 提交于 2019-12-17 16:08:16
问题 I have a table in the db, which has the following :- CountryID, CountryName, and CountryImage. Now I am trying to display the image in the index and I have the following in the View :- <td> @if (item.Image != null) { <img src="@Model.GetImage(item.Image)" alt="@item.CountryName"/> } and then in the ViewModel I have :- public FileContentResult GetImage(byte[] image) { if (image != null) return new FileContentResult(image, "image/jpeg"); else { return null; } } However I cannot see the image

MVC Razor, Include JS / CSS files from another project

混江龙づ霸主 提交于 2019-12-17 16:07:13
问题 I've got a C# MVC project that uses Razor syntax. To be able to reuse some code, I want to put some of my JavaScript and CSS files in a different project and include them somehow. This is how my scripts are included at the moment: <script src="@Url.Content("~/Scripts/bootstrap-typeahead.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/bootstrap-dropdown.js")" type="text/javascript"></script> At the moment, the scripts are in the same project as the cshtml file but

How do I set a checkbox in razor view?

我是研究僧i 提交于 2019-12-17 16:04:02
问题 I need to check a checkbox by default: I tried all of these, nothing is checking my checkbox - @Html.CheckBoxFor(m => m.AllowRating, new { @value = "true" }) @Html.CheckBoxFor(m => m.AllowRating, new { @checked = "true" }) @Html.CheckBoxFor(m => m.AllowRating, new { @checked = true }) @Html.CheckBoxFor(m => m.AllowRating, new { @checked = "checked"}) 回答1: You should set the AllowRating property to true , preferably in the controller or model. Like other inputs, the checkbox's state reflects

How to get the column titles from the Display(Name=) DataAnnotation for a strongly typed list scaffold view at runtime?

混江龙づ霸主 提交于 2019-12-17 15:44:31
问题 How do I get the [Display(Name="Some Title")] DataAnnotations "Some Title" rendered in the List scaffold view's output? I create a strongly typed list scaffold view for this class: public class CompanyHoliday { [Key] public int Id { get; set; } [Required] [Display(Name = "Datum")] [DataType(System.ComponentModel.DataAnnotations.DataType.Date)] public DateTime Date { get; set; } [Required] [StringLength(50)] [Display(Name = "Feiertag Name")] public string Name { get; set; } } The view looks

How to access javascript variable within @URL.Action()

China☆狼群 提交于 2019-12-17 15:30:40
问题 How can I access JavaScript value inside @URL.Action() ? something like: <script type="text/javascript"> function name(myjavascriptID) { jQuery("#list_d").jqGrid('setGridParam', { url: '@URL.Action("download file", "download", new { id = <myjavascriptID> })', page: 1 }); } </script> 回答1: You can't. JavaScript doesn't execute when generating the action URL. What you can do, is do something like this: function name(myjavascriptID) { var link = '@Url.Action("download file", "download", new { id

Why do I get null instead of empty string when receiving POST request in from Razor View?

╄→гoц情女王★ 提交于 2019-12-17 15:25:11
问题 I used to receive empty string when there was no value: [HttpPost] public ActionResult Add(string text) { // text is "" when there's no value provided by user } But now I'm passing a model [HttpPost] public ActionResult Add(SomeModel Model) { // model.Text is null when there's no value provided by user } So I have to use the ?? "" operator. Why is this happening? 回答1: You can use the DisplayFormat attribute on the property of your model class: [DisplayFormat(ConvertEmptyStringToNull = false)]

@Html.BeginForm Displaying “System.Web.Mvc.Html.MvcForm” on Page

烂漫一生 提交于 2019-12-17 10:57:39
问题 I have a razor view that I added a delete button to inside of an 'if' statement and when the view is rendered in the browser it is displaying "System.Web.Mvc.Html.MvcForm" next to the delete button. How do I get rid of it? Here is the code: <div id="deletestatusupdate"> @if (update.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)) { @Html.BeginForm("deleteupdate", "home") @Html.Hidden("returnUrl", Request.Url.ToString()) <button name="id" value="@update

Render Razor view to string in ASP.NET 5

一个人想着一个人 提交于 2019-12-17 10:46:09
问题 In previous versions of ASP.NET it was possible, although not very simple, to render Razor views as strings. The methods I've seem are to use a fake controller, or also to use some external engine like RazorEngine. Now, many things are changed with ASP.NET 5 and I was wondering whether or not this is now simpler than before. So in the new version of the framework is there one straightforward way to render Razor views as strings or we still need to use the methods from the previous versions?