mvc-editor-templates

Limit JavaScript and CSS files on ASP.NET MVC 2 Master Page based on Model and View content

佐手、 提交于 2019-12-11 14:08:24
问题 I want to include certain .js and .css files only on pages that need them . For example, my EditorTemplate DateTime.ascx needs files anytimec.js and anytimec.css . That template is applied whenever I use either the EditorFor or EditorForModel helper methods in a view for a model with a DateTime type value. My technique: I've put this condition into the <head> section of my master page. It checks for a DateTime type property in the ModelMetadata. <% if (this.ViewData.ModelMetadata.Properties

How to work with nested property in tinyMCE in ASP.NET MVC

橙三吉。 提交于 2019-12-11 04:18:36
问题 Using the tiny MCE editor template in ASP.NET MVC provided as sample via Nuget. In this template there is a call to tinymce method as below: $('#@ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)').tinymce({ . . . }); At runtime this changes to $('#fieldId').tinymce({ . . . }); It was working fine until the property which this was targeting was in the model itself. But when I moved the property inside another property, it stopped working. Now the field is like ModelView.SomeModel

How can I make a fancy checkbox template for ASP.NET Core?

偶尔善良 提交于 2019-12-11 01:58:47
问题 I've got a lot of boolean s in my model, and we're using Bootstrap, so for every boolean property I'm copy/paste refactoring: <div class="form-group"> <div class="custom-control custom-checkbox "> <input asp-for="IsFoo"/> <label asp-for="IsFoo"></label> </div> </div> ... but that's dumb. I tried adding this to Views/Shared/EditorTemplates/bool.cshtml : @model bool? <div class="form-group"> <div class="custom-control custom-checkbox "> <input asp-for="@Model"/> <label asp-for="@ViewData

Setting default selected value of selectlist inside an editor template

╄→гoц情女王★ 提交于 2019-12-10 17:46:20
问题 I have this code to set the default value of my select list: public ActionResult Register() { IList<Country> countryList = _countryRepository.GetAllCountry(); var registerViewModel = new RegisterViewModel { CountryId = 52, CountryList = new SelectList(countryList, "CountryId", "CountryName", "Select Country") }; return View(registerViewModel); } I have this on my view and this works well sets the selected country value to 52: <%: Html.DropDownListFor(model => model.CountryId, Model

ASP.NET MVC - Pass outer model to editor template

只谈情不闲聊 提交于 2019-12-10 16:28:27
问题 I'm using custom editor templates for most of my forms, but while attempting to use them to render a custom object picker I've found no easy way to pass information about the containing context to the editor template. Specifically, my main form renders an edit for a domain object, and the editor template needs to render an AJAX picker which contains a list of objects which are dependent on the ID of the domain object. I'm passing the ID using the additionalViewData parameter currently, which

DatePicker Editor Template

空扰寡人 提交于 2019-12-09 13:40:08
问题 Below is an EditorTemplate that renders a Bootstrap datetimepicker with EditorFor helpers, the problem I am seeing is with the script section. It works OK for one DateTimePicker per view - but since I am using class selector, whenever I use 2 or more DateTimePicker s per view it renders duplicate <script> sections, confusing the DOM as to on which TextBox to invoke the calendar. What am I missing here? @model DateTime? <div class='input-group date datePicker'> <span class="input-group-sm">

Passing extra data to EditorTemplate

廉价感情. 提交于 2019-12-08 15:44:26
问题 Lets say I have a LineItem (from the over used Shopping Cart example) and I want to render it using a EditorTemplate. I am perfectly alright rendering it using a @Html.EditorFor(m => m.LineItems) from the parent view (partial or otherwise), but what is confusing is the best approach to pass some extra data (say a SelectList which has data coming in from the database) to the template. Clearly I should not be polluting the LineItem model by adding these extraneous items (which are however

Move Html.DropDownListFor into EditorTemplate

ぐ巨炮叔叔 提交于 2019-12-08 09:18:21
问题 Trying to create an editor template using a dropdownlist in MVC4. I can get the dropdownlistfor to work directly in the view as such: @Html.DropDownListFor(model => model.Item.OwnerId, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText")) But then to "generify" it and put it into an editor template, I cannot get it to work. Here is what I am trying in my EditorTemplate partial: @Html.DropDownListFor(model => model, new SelectList(Model.DDLOptions.CustomerOptions, "Value",

MVC3 Razor Editor/Display templates and generics

拜拜、爱过 提交于 2019-12-07 15:38:04
问题 There were a few questions regarding mvc templates and generics however there seems to be none related to what I'm looking for. Consider the following models: namespace MyNamespace { public class ModelBase { /* Not important for example */ } public class MyModel : ModelBase { public string Name { get; set; } } public class MyViewModel { public IEnumerable<ModelBase> Data { get; set; } } } And a controller: public class HomeController : Controller { public ActionResult Index { return View(new

Model binding a list of checkboxes problem

折月煮酒 提交于 2019-12-07 04:49:46
问题 In my EditorTemplates, i Have two Views. One for my Category (called _Category) @model com.example.Models._Category @Html.CheckBox(Model.Name, Model.Selected) @Html.LabelFor(c => c.Name, Model.Name) <br /> and one for the List of Categories (called _Categories) @model List<com.example.Models._Category> @for (int i = 0; i < Model.Count; i++) { @Html.EditorFor(c => Model[i]); } In the view that shows these categories, i have a list of Categories which is being used like so: @Html.EditorFor(m =>