简体   繁体   English

使用相同模型的MVC4动态视图

[英]MVC4 dynamic view using the same model

I am trying to create a page that can edit the properties of an item, for example Employees 我正在尝试创建一个页面,该页面可以编辑项目的属性,例如员工

public class Employee
{
    //user information
    public int ID { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    [MaxLength(20)]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required")]
    [MaxLength(10)]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Hometown is required")]
    [MaxLength(10)]
    public string HomeTown { get; set; }

    //optional
    public string SpouseName { get; set; }
    public string KidNameOne { get; set; }
    public string KidNameTwo { get; set; }

    //your information
    [Required(ErrorMessage = "Your name is required")]
    [MaxLength(20)]
    public string YourName { get; set; }

    [Required(ErrorMessage = "Your address is required")]
    [MaxLength(100)]
    public string YourAddress { get; set; }

}

Now I know how to create a view or controller to edit the employee. 现在,我知道如何创建视图或控制器来编辑员工。
But now I want to simplify the page by separating the optional information with the absolute necessary information. 但是现在我想通过将可选信息与绝对必要信息分开来简化页面。
For example on top of the page have a dropdownlist which has the option of Necessary information and optional information. 例如,在页面顶部有一个下拉列表,其中包含必需信息和可选信息选项。
If I select necessary information only firstname lastname and hometown will get displayed for users to edit or view. 如果我选择必要的信息,则只会显示名字的姓氏和家乡,以供用户编辑或查看。
If I select optional everything else will display (excluding the necessary ones). 如果选择可选,则将显示其他所有内容(必要的除外)。

I thought of partial view but not sure if it would be any use. 我想到了局部视图,但不确定是否会有用。 I just need a direction of how to achieve that. 我只需要一个实现该目标的方向。 Because I have never done anything that has anything to do with dynamic view with razor code in MVC. 因为我从来没有做过任何与MVC中带有剃刀代码的动态视图有关的事情。

I need suggestions on how do I approach this issue. 我需要有关如何解决此问题的建议。

So the user should be able to choose whether he wants to see the optional fields? 因此,用户应该能够选择是否要查看可选字段? That should probably be handled by client-side JavaScript, not something MVC-specific. 这可能应该由客户端JavaScript处理,而不是MVC特定的处理。

Here is a simple "expander"-like solution using jQuery: 这是一个使用jQuery的简单的类似于“扩展器”的解决方案:

<ul>
    <li>
        @Html.LabelFor(m => m.FirstName)
        @Html.TextBoxFor(m => m.FirstName)
    </li>
    <li>
        @Html.LabelFor(m => m.LastName)
        @Html.TextBoxFor(m => m.LastName)
    </li>
    ...
</ul>

<a href="#" onclick="toggleOptional()" >Optional info..</a>
<ul id="optional" >
    <li>
        @Html.LabelFor(m => m.KidNameOne)
        @Html.TextBoxFor(m => m.KidNameOne)
    </li>
    <li>
        @Html.LabelFor(m => m.KidNameTwo)
        @Html.TextBoxFor(m => m.KidNameTwo)
    </li>
    ...
</ul>

<script type="text/javascript" >
    function toggleOptional() {
        $('#optional').toggle();
    }

    //Runs on jQuery.ready:
    $(function () {
        $('#optional').hide();
    });
</script>

jsfiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM