Saturday, 15 May 2010

c# - Using HtmlHelper for inner Model properties -


I have such an ideal structure for my ASP.NET MVC view:

  Public Class MyModel {Public Other Models 1 Prop 1 (Set; Set;} Public One and Model 2 Prop 2 {Set; Set;}} Public Class Other Models 1 {Public Inner Model InnerProp 1 {Set; Set;} Public Inner Model InnerProp 2 {Set; Set;} Public Inner Model InnerProp 3 {Other Models of the Public Class 2 {Public Inner Modell Innerp Pop 1} meet; set;} public Inner Model InnerProp2 {set; set;}} Public class Inner model {public string input {get} set;}}  

This means, Normal model is to pass to see MyModel. In some other models, another model 1, another model 2, in which the inner model is included in the form of properties.

I have a helper I want to create a function which is how to present the inner model then on my page See me have the ability to write something like RenderInnerModel (m => m.Prop1.InnerProp1)

The problem is, I do not know how to pass the htmlhelper in my function inside my assistant Ability to use @ Html.TextBoxFor (m => m.Input)

While problem can be solved using partial sequences per inner model, I should standardize the support function and do not want to mess with complex models for a partial example of Tuple etc.


Edit

While almost what I want for the editor, I still want to understand that it is possible to do something Like

  var model = new HTML helper & lt; Inner MODEL & gt; (View Contex, this);  

But in the context of MyModel? Thanks!

Your Controller:

  Public Class Other Models {Public Inner Models InnerProp 1 {Get; Set; } Receive public Inner Model InnerProp2 { Set; }} Public class inramodel {public string input {get; Set; }} Public Class Editor ExampleController: Controller {// // GET: / EditorExample / Public Performance Index () {Another Model Model = New Other Model (); Model.InnerProp1 = New Inner Model {Input = "Test 1"}; Model.InnerProp2 = New Inner Model {input = "test2"}; See Return (Model); }}  

Your view:

  @model MVCApp.Controllers.AnotherModel & lt; H2 & gt; Editor Template & lt; / H2 & gt; @ Html.EditorFor (_ = & gt; _.InnerProp1) @ Html.EditorFor (_ = & gt; _.InnerProp2)  

In the "Share" folder a new "View" Create a folder Call "Editor Templates", then add a partial view "Inner Mode. CSSM":

  @model MVCApp.Controllers.InnerModel @if (model.input == "test2 ") {& Lt; H4 & gt; @ Model.Input & lt; / H4 & gt; } Other {& lt; H2 & gt; @model Input & lt; / H2 & gt; }  

This is a simple implementation, if you want me to be able to post a custom HtmlHelper


No comments:

Post a Comment