簡體   English   中英

剃刀視圖模型中字段的條件視圖

[英]Conditional view of fields in a view model in razor

我有以下視圖模型來查詢我的表:

QuestionViewModel.cs

public enum TypeQuestion {
   Long = 1,
   Short = 2,
   Small = 3,
}

public class QuestionViewModel
{

    public string Name { get; set; }

    public string LastName { get; set; }

    public string Address { get; set; }

    public string MaxAge { get; set; }

    public string Category { get; set; }

    public string Account { get; set; }

    public TypeQuestion CurrentTypeQuestion { get; set; }
}

如果我正在查詢的類型是:

長:顯示所有字段。
短:顯示姓名,姓氏,地址,最大年齡。
小:顯示名稱,姓氏。

有什么方法可以放置某種DataAnnotation來確定要在視圖中顯示的字段,還是可以通過其他方式來避免? 對於每個領域。

謝謝。

這可能是矯kill過正,實際上我傾向於@Mystere Man的回答,但這是另一種選擇。

代替您的ViewModel中的常規原始類型,將它們設置為滿足邏輯。 看起來總是顯示NameLastName ,而AddressMaxAge是有條件的。

因此,像這樣設置您的ViewModel:

public class QuestionViewModel
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public IEnumerable<ConditionalField> ConditionalFields { get; set; }
    public string Category { get; set; }
    public string Account { get; set; }
}

public class ConditionalField
{
   public string Field { get; set; }
   public bool Display { get; set; }
}

在您的控制器中,根據CurrentTypeQuestion的值,設置嵌套的viewmodel以及AddressMaxAge的布爾值。

然后,使您的視圖如下所示:

/Views/Questions.cshtml

@model QuestionViewModel
@Html.DisplayForModel()

然后為QuestionViewModel創建一個自定義顯示模板(或編輯器模板,如果這是一種形式):

/Views/DisplayTemplates/QuestionViewModel.cshtml

@model QuestionViewModel
@Html.DisplayFor(model => model.Name)
@Html.DisplayFor(model => model.LastName )
@Html.DisplayFor(model => model.Category)
@Html.DisplayFor(model => model.Account)
@Html.DisplayFor(model => model.ConditionalFields)

然后為ConditionalField創建另一個自定義顯示模板:

查看/ DisplayTemplates / ConditionalField.cshtml

@model ConditionalField
@if (Model.Display) {
   @Html.DisplayForModel()
}

就像我說的那樣,可能有些矯over過正,但是最后,您在自定義模板中只有一個if語句,沒有循環,並且您的主視圖和第一層模板保持整潔。

為了保持簡單,並避免視圖中的邏輯復雜,只需創建三個不同的視圖,每個視圖中只包含所需的數據即可。 然后根據問題類型在控制器中選擇視圖。

基於此鏈接和此鏈接

控制器:

public ActionResult Consulta()
{
    return View(new QuestionViewModel());
}

視圖模型:

public enum TypeQuestion {
   Long = 1,
   Short = 2,
   Small = 3,
}

public class QuestionViewModel
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public int MaxAge { get; set; }
    public string Category { get; set; }
    public string Account { get; set; }
    public TypeQuestion CurrentTypeQuestion { get; set; }

    public bool EnabledField(ModelMetadata field)
    {
        //check pending implementation
        return true;
    }
}

視圖:

@model MySite.QuestionViewModel
@using System.Linq;
@using System.Collections;

@{
    ViewBag.Title = "Question";
    Layout = "~/Views/Shared/Layout.cshtml";
}
<h2>Question</h2>

@using (Html.BeginForm(new { id = "FormQuestion" }))
{

    foreach (var prop in ViewData.ModelMetadata.Properties
        .Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm) && ViewData.Model.EnabledField(pm)))
    {
        if (prop.HideSurroundingHtml)
        {
            Html.Editor(prop.PropertyName);
        }
        else
        {
            <div class="editor-label">
                @(prop.IsRequired ? "*" : "")
                @Html.Label(prop.PropertyName)
            </div>
            <div class="editor-field">
                @Html.Editor(prop.PropertyName, prop.Model)
                @Html.ValidationMessage(prop.PropertyName, "*")
            </div>
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM