简体   繁体   English

MVC视图用于处理一对多关系

[英]MVC View for Manipulating One-to-Many Relationships

I have an ASP.NET MVC application with a one-to-many relationship WITHOUT lookups (ie the user can input their own data). 我有一个ASP.NET MVC应用程序,它具有一对多的关系而无需查找(即用户可以输入自己的数据)。 I'm trying to determine the best UI for adding records. 我正在尝试确定添加记录的最佳UI。 Here is what I have right now: 这是我现在所拥有的:

Table1
------
eventId
eventName

Table2
------
eventRepeatId
eventId
startTime
endTime

Table1 & Table2 are related through the eventId field. 表1和表2通过eventId字段关联。 On my view, what I'm doing right now is tying the Table2 data to a multiselect box. 在我看来,我现在正在做的就是将Table2数据绑定到多选框。 The user clicks on a jQuery calendar, chooses a start and end time, and clicks an "add" button. 用户单击jQuery日历,选择开始和结束时间,然后单击“添加”按钮。 This adds the new start/end time as an option to the multiselect box. 这会将新的开始/结束时间作为选项添加到多选框。 Then, in my viewmodel method, I'm parsing the option and saving it to the DB. 然后,在我的viewmodel方法中,我解析该选项并将其保存到数据库中。

This works currently. 目前适用。 However, the caveat is that you have to have all of the options selected in the multiselect box when you hit "Create/Update". 但是,需要注意的是,当您单击“创建/更新”时,必须在多重选择框中选择所有选项。 Otherwise, the data posted to the controller is null. 否则,发布到控制器的数据为空。

I'd like to find a method of adding and removing a number of start/end times to the event so that when I hit "Create/Update", they are all passed to the controller regardless of whether I have them selected or not. 我想找到一种添加和删除事件开始/结束时间的方法,以便在单击“创建/更新”时将它们全部传递给控制器​​,而不管是否选择了它们。

Right now I'm leaning towards using jQuery to add/remove HTML elements (tables or divs) displaying the user's selections, and storing the data itself as JSON in a hidden form field. 现在,我倾向于使用jQuery添加/删除显示用户选择的HTML元素(表或div),并将数据本身作为JSON存储在隐藏的表单字段中。 Just wondering if there's a better way. 只是想知道是否有更好的方法。

How data is posting to controller. 数据如何发布到控制器。 R u using full postack when form post. 表格发布时,您要使用完整的Postack。 Try to use ajax functionality to post form data. 尝试使用ajax功能发布表单数据。

try to add remove start and end time through java script. 尝试通过Java脚本添加删除开始和结束时间。 So when u post the form data then all selection will persist. 因此,当您发布表单数据时,所有选择都将保留。 Thanks 谢谢

Solution (I ended up using jQuery and a hidden field): 解决方案(我最终使用了jQuery和一个隐藏字段):

<div class="col-xs-5">
    <div id="dtpIS" class="row datetimepicker input-group">
        <div class="input-group">
            <input class="form-control" id="IrregularDPStart" data-format="MM/dd/yyyy HH:mm PP" />
            <span class="input-group-addon add-on">
                <i data-time-icon="glyphicon glyphicon-time" data-date-icon="glyphicon glyphicon-calendar">
                </i>
            </span>
        </div>
    </div>
    <div id="dtpIE" class="row datetimepicker input-group">
        <div class="input-group">
            <input class="form-control" id="IrregularDPEnd" data-format="MM/dd/yyyy HH:mm PP" />
            <span class="input-group-addon add-on">
                <i data-time-icon="glyphicon glyphicon-time" data-date-icon="glyphicon glyphicon-calendar">
                </i>
            </span>
        </div>
    </div>
</div>
<div class="col-xs-1">
    <span class="glyphicon glyphicon-chevron-right" id="IrregularAdd"></span>
</div>
<div class="col-xs-6">
    <table id="IDDTable" class="table table-striped">
        <thead>
            <tr>
                <th>Start Time</th>
                <th>End Time</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <input type="hidden" id="EventIrregularRepeats" name="EventIrregularRepeats" />
</div>

<script type="text/javascript">
    var idd = [];
    function UpdateIdd() {
        idd = [];
        $("#IDDTable tbody tr").each(function () {
            var dates = [];
            var count = 0;
            $(this).child("td").each(function () {
                dates[count] = $(this).text();
                count++;
            })
            var newidd = { startTime: dates[0], endTime: dates[1] };
            idd.push(newidd);
        });

        $("#EventIrregularRepeats").val(JSON.stringify(idd));
    }
    $(function () {
        $("#IrregularAdd").on('click', function () {
            var tbody = $("#IDDTable tbody");
            var st = $("#IrregularDPStart").val();
            var end = $("#IrregularDPEnd").val();
            tbody.append("<tr><td>" + st + "</td><td>" + end + "</td><td><span class='iddremove glyphicon glyphicon-remove'></span></td>");
            UpdateIdd();
        })
        $('#IDDTable').on('click', '.iddremove', function () {
            $(this).parents("tr").remove();
            UpdateIdd();
        });
    });
</script>

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

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