2010-10-23 6 views

Répondre

3

Vous pouvez obtenir la même chose avec le moteur de vue Razor.

Modèle:

public class MyViewModel 
{ 
    public string Value { get; set; } 
} 

Controller:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyViewModel 
     { 
      Value = "foo" 
     }; 
     return View(model); 
    } 
} 

Vues:

~/Views/Home/Index.cshtml:

@model MyApp.Models.MyViewModel 

@{ Html.BeginForm(); } 

    @Html.EditorFor(x => x.Value) 
    <input type="submit" value="OK" /> 

@{ Html.EndForm(); } 

~/Views/Home/EditorTemplates/Template.cshtml:

<p>Some text before template</p> 
@RenderBody() 
<p>Some text after template</p> 

~/Views/Home/EditorTemplates/string.cshtml:

@model System.String 
@{ 
    Layout = "~/Views/Home/EditorTemplates/Template.cshtml"; 
} 
<div>@Html.TextBoxFor(x => x)</div> 

Remarquez comment le modèle de l'éditeur string a été personnalisé et la mise en page Template.cshtml utilisé comme maître.

Questions connexes