mvc-editor-templates

Create a generic repository DropDown with SelectListItem in MVC

╄→尐↘猪︶ㄣ 提交于 2019-12-31 06:58:27
问题 Having returned to this problem after a few months I've added my current best answer below. In the original question I was still looking for a simple way to achieve a generic DropDown but the title was more closely tied to the specific error I was then facing. I've amended the title to reflect the answer more closely. Hopefully this might help someone. Original Question: Generic Editor Template for DropDownListFor throws Cannot convert type error I'm trying to create a generic template for a

EditorTemplate Naming Convention for Collection model classes

ε祈祈猫儿з 提交于 2019-12-31 02:58:11
问题 I have a model property that is declared as type List<MyClass> . public class MyModel { List<MyClass> MyProperty { get; set; } } I want to be able to display/edit the property using Razor templates. My question is, how I name an EditorTemplate view so that I can display the property using the normal syntax: @model MyModel @Html.DisplayFor(m => m.MyProperty) I know that I can create a view called MyClass.cshtml that will be used for the type MyClass , but how do I create a template for the

Too many JavaScript and CSS files on my ASP.NET MVC 2 Master Page?

喜欢而已 提交于 2019-12-30 11:41:29
问题 I'm using an EditorTemplate DateTime.ascx in my ASP.NET MVC 2 project. <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %> <%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %> <script type="text/javascript"> $(function () { $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({ format: "%c/%d/%Y %l:%i %p" }); }); </script> This uses the Any+Time™ JavaScript library for jQuery by Andrew M. Andrews III. I've added

MVC can't override EditorTemplate name when used in EditorFor for child object

核能气质少年 提交于 2019-12-29 18:53:21
问题 I am trying to use an EditorTemplate to display a child collection in a table in the parent’s view. The problem I have run into is that this only seems to work if the template is named exactly the same as child’s class. When I attempt to use a template with a slightly different name, and pass that name as the templateName argument to EditorFor,I get a runtime error. I was hoping I could use different child EditorTemplates for different purposes with the same child collection. Here is an

How to make a default editor template for enums?

有些话、适合烂在心里 提交于 2019-12-28 09:18:33
问题 How can I make a default editor template for enums? By which I mean: can I do something like this: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Enum>" %> <% -- any code to read the enum and write a dropdown --> And put this in the EditorTemplates folder under the name Enum.ascx ? Here's a workaround for my problem that I tried, but it's not what I need. Here is my Enum: public enum GenderEnum { /// <summary> /// Male /// </summary> [Description("Male Person")] Male, ///

Problem binding selected value to DropDownListFor inside Editor Template

允我心安 提交于 2019-12-25 08:26:03
问题 Description I have a payment page that includes a form for entering bank account information. I have encapsulated the bank account information into a Model / Editor Template. The page itself has its own View Model, which happens to contain a BankAccount property to be passed in to the Editor. [[View Models]] public class PaymentPageModel { public SomeProperty1 { get; set; } public SomeProperty2 { get; set; } public BankAccount BankAccount { get; set; } ... } public class BankAccount { public

IEnumerable property with MVC3 EditorTemplate

痞子三分冷 提交于 2019-12-24 15:01:21
问题 Similar to this post IEnumerable model property in an ASP.NET MVC 3 Editor Template, I have Model public class Student { public int StudentId { get; set; } public string StudentName{ get; set; } //FYI..Its virtual because of EF relationship public virtual ICollection<Class> Classes{ get; set; } } public class Class { public int ClassId { get; set; } public string ClassName{ get; set; } } View - EditStudent @model Student @Html.TextBoxFor(m => m.StudentName) //I get the error for following.

Posting a list of view models in a model in ASP.NET MVC 4

可紊 提交于 2019-12-24 08:14:47
问题 So I am trying to post a view with a model that contains a list of models, but when posting, only the first model in the list of models show their Id. I am using the editorFor to show my models through the editor template. Notice in the template I have the hiddenFor for the Id value. Why when i post the form it only shows the first model in the list? How do I fix this so that the post will contain all the Id values in the list of models being posted? Its able to post the values for the drop

Extend HiddenFor Templates in ASP.NET MVC

一世执手 提交于 2019-12-23 10:15:28
问题 I thought Html.HiddenFor could use Templates like Html.DisplayFor or Html.EditorFor . Unfortunately the method doesn't accept a TemplateName like the others. I know, the workaround would be to use a DisplayFor/EditorFor Template which has HiddenFors. But I would like to find out how to extend the Html.HiddenFor method. Anyone? Regards 回答1: Seems like you are mislead by wrong analogy. HiddenFor corresponds exactly to the <input type="hidden"/> tag. Just like TextBoxFor , CheckBoxFor etc. These

ASP.NET MVC 4 - EditorTemplate for nested collections

怎甘沉沦 提交于 2019-12-22 14:27:11
问题 I have the following model classes (classes simplifies for the purpose of this question): public class Lesson { public Guid Id {get;set;} public string Name {get;set;} public List<ExerciseForPupil> Exercises {get;set;} } public class ExerciseForPupil { public Guid Id {get;set;} public string Name {get;set;} public List<ExerciseItemForPupil> ExerciseItems {get;set;} } public class ExerciseItemForPupil { public Guid Id {get;set;} public string Content {get;set;} public string UserValue {get;set