繁体   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