razor

ASP.NET MVC. How use DisplayNameFor in order to create a table heading and body?

半腔热情 提交于 2019-12-21 12:37:17
问题 How get a property display name using DisplayNameFor() to build a table header. for instance: @model IEnumerable<Item> <table class="table"> <thead> <tr> <td>@Html.DisplayNameFor(? => ?.prop1)</td> <td>@Html.DisplayNameFor(? => ?.prop2)</td> <td>@Html.DisplayNameFor(? => ?.prop3)</td> </tr> </thead> <tbody> @foreach (Item item in Model) { <tr> <td>@Html.DisplayFor(i => item.prop1)</td> <td>@Html.DisplayFor(i => item.prop2)</td> <td>@Html.DisplayFor(i => item.prop3)</td> </tr> } </tbody> <

How to add and use a razor editor template

吃可爱长大的小学妹 提交于 2019-12-21 12:35:16
问题 I have tried to find documentation or samples on how to incorporate razor editor templates into a project. the following answer on another thread may fix a problem I am having but I do not know how to incorporate it into my project. It is no doubt easy. here is the solution I do not know how to incorporate: Client-side validation of input type date does not work 回答1: Here is short info about adding EditorTemplate . In short words - you have to create directory EditorTemplates in Views/Shared

MVC/Razor model binding collections when an element is missing

谁都会走 提交于 2019-12-21 12:17:34
问题 I've got a form that contains a variable length list of textboxes, rendered using a template similar to this.. @Html.TextBox("items[" + itemIndex + "].Title", someValue) So the final rendered HTML looks something like this... <input id="items_0__Amount" type="text" value="Apple" name="items[0].Title"> <input id="items_1__Amount" type="text" value="Banana" name="items[1].Title"> <input id="items_2__Amount" type="text" value="Orange" name="items[2].Title"> On form submission this binds to my

ASP.NET MVC 3 - Add/Remove from Collection Before Posting

[亡魂溺海] 提交于 2019-12-21 12:15:06
问题 I have a model that contains a collection, such as this: class MyModel { public List<MySubModel> SubModels { get; set; } } In the view, I want to dynamically add/remove from this list using Javascript before submitting. Right now I have this: $("#new-submodel").click(function () { var i = $("#submodels").children().size(); var html = '<div>\ <label for="SubModels[' + i + '].SomeProperty">SomeProperty</label>\ <input name="SubModels[' + i + '].SomeProperty" type="textbox" />\ </div>' $("

Razor Pages, form page handler not working with GET method

懵懂的女人 提交于 2019-12-21 12:10:36
问题 I have a small ASP.NET Core 2.1 Razor Pages project. I'm making a simple list display page with a basic search functionality. In my model, I have 4 page handlers (2 of them are added for debug purposes): public async Task OnGetAsync() { Posting = await _context.Postings .Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync(); } public async Task OnPostAsync() { Posting = await _context.Postings .Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync(); } public async Task

Where should I include a script for a view component?

只愿长相守 提交于 2019-12-21 12:08:38
问题 I have tried adding a section script inside a view component's view. @section scripts { <script src="~/somepath" asp-append-version="true"></script> } I also have the Render Section in the shared layout @RenderSection("scripts", required: false) When used in partial views and elsewhere in the project the script loads fine. However when in a View Component the script does not load. I suppose I could include the script in the section tag of every view that calls the component. I feel this does

Trying to add JS and CSS to layout file in MVC 3 Razor website from partial views

天涯浪子 提交于 2019-12-21 11:03:17
问题 Im currently using a method that looks like the following code to add script and css files to the head of the Layout file. public static class HtmlHelperExtensions { public static MyCompanyHtmlHelpers MyCompany(this HtmlHelper htmlHelper) { return MyCompanyHtmlHelpers.GetInstance(htmlHelper); } } public class MyCompanyHtmlHelpers { private static MyCompanyHtmlHelpers _instance; public static MyCompanyHtmlHelpers GetInstance(HtmlHelper htmlHelper) { if (_instance == null) _instance = new

@Html.Raw insists on encoding quotes

99封情书 提交于 2019-12-21 10:58:51
问题 MVC4 razor view. Given that a string backgroundImage is set like this: backgroundImage = string.Format("background: url('{0}') top left no-repeat;", project.MainImage); Why does this <div class="spotlight slide teaser-text" id="@slideId" style="@Html.Raw(backgroundImage)"> produce <div class="spotlight slide teaser-text" id="spotlight-0" style="background: url('/media/215/spotlight01.jpg') top left no-repeat;"> Html.Raw , new MvcHtmlString and MvcHtmlString.Create all behave similarly. I

In .NET MVC, is there an easy way to check if I'm on the home page?

半城伤御伤魂 提交于 2019-12-21 10:52:40
问题 I need to take a particular action if a user logs in from the home page. In my LogOnModel, I have a hidden field: @Html.Hidden("returnUrl", Request.Url.AbsoluteUri) In my Controller, I need to check if that value is the Home page or not. In the example below, I'm checking to see if the user is on a particular page ("Account/ResetPassword"). Is there a way to check to see if they're on the home page without resorting to regular expressions? [HttpPost] public ActionResult LogOnInt(LogOnModel

What is the best practice to include js/css files in an enterprise application framework?

纵然是瞬间 提交于 2019-12-21 09:18:46
问题 I am working on an enterprise application development in ASP.NET MVC3. Of-course I have different master layouts and multiple views. My concerns Including all js/css files in master layout might affect the performance of the page Including the files in views (where it is required) are creating duplicate references (kick-off jquery/other libraries) More the references, the more the back&forth requests between client and server - which in turn affect the performance of the output page My