2015-10-06 120 views
1

我有一個創建這樣的表單字段一個強類型的視圖:MVC強類型的視圖數據

@Html.TextBoxFor(x => x.Name, new { @class = "form-control" }) 

這很好,X在上面的lambda表達式涉及到我和的屬性創建一個類'名稱'。

如何使用此相同的視圖,但可以訪問不同類的屬性?例如,如果你想象我有另一個叫做UserDetails的類,並且電子郵件地址是它的一個屬性。我該怎麼做:

@Html.TextBoxFor(x => x.Email, new { @class = "form-control" }) 

在同一個強類型的視圖?

+0

您可以使用您的行動,調用相應的視圖,它是確定通過新模型一起新的看法! –

+0

我不確定你的意思,我如何在新視圖中傳遞新模型? –

+0

您可以讓基類具有共同屬性的兩種模型,然後爲此設置視圖,然後在您的視圖中可以投射類型並加載另一個局部視圖 –

回答

2

在asp.net mvc的傳遞模型查看這樣的:

public ActionResult Base() 
     { 
      return View(new DerviedOne()); 
     } 

這裏是你的模型定義:

public class BaseModel 
    { 
     public int Id { get; set; } 
    } 

    public class DerviedOne : BaseModel 
    { 
     public string Email { get; set; } 
    } 

    public class DerviedTwo : BaseModel 
    { 
     public string Name { get; set; } 
    } 

然後,你必須創建三個觀點:

基本視圖:

@using Models 
@model Models.BaseModel 

@{ 
    ViewBag.Title = "Base"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Base</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>BaseModel</h4> 
     <hr/> 
     @Html.ValidationSummary(true, "", new {@class = "text-danger"}) 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       @Html.TextBoxFor(x=>x.Id) 
       <input type="submit" value="Create" class="btn btn-default"/> 
      </div> 
     </div> 
    </div> 

    if(Model is DerviedOne) 
    { 
     Html.RenderPartial("DerviedOneView", Model as DerviedOne); 
    } 

    if (Model is DerviedTwo) 
    { 
     Html.RenderPartial("DerviedTwoView", Model as DerviedTwo); 
    } 
} 

第二種觀點:

@model WebApplication.Models.DerviedOne 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>DerviedOne</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

第三種觀點:

@model WebApplication.Models.DerviedTwo 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>DerviedTwo</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 
+1

Perfect Thanks! –