0

我认为我可以添加“操作”。动作可以是电子邮件或短信。如果我采取行动,我会在视图中显示一个表格。因此,如果我有 5 个操作并按下提交按钮,那么我希望将操作发送到模型。

我的观点:

<table id="action-detail-table" class="table table-striped table-bordered detail-table hide">
    <thead id="headActions"></thead>
    <tbody id="allActions"></tbody>
</table>
@Html.HiddenFor(m => m.Actions.Type, new { id = "action-types-type", name = "action-types-type" })
@Html.HiddenFor(m => m.Actions.Contact, new { id = "action-types-contact", name = "action-types-contact" })

视图中的 Javascript:

$('#addAlertModal form').submit(function (e) {
        var actionType = new Array();
        var contact = new Array();

        $('#allActions tr').each(function () {
            actionType.push(JSON.stringify($(this).find("td:eq(0)").text()));
            contact.push(JSON.stringify($(this).find("td:eq(1)").text()));
        });

        $('#action-types-type').val(actionType);
        $('#action-types-contact').val(contact);
    });

function addAction() {
    if ($.trim($('#allActions').find('td').text()).length == 0) {
        $('#headActions').append("<tr><th>Actie</th><th>Naar</th></tr>");
    }

    if ($('#action-type').val() == "Email") {
        $('#allActions').append($('#allActions').innerHTML + "<tr><td> Email </td><td>" + $('#action-email').val() + "</td></tr>");
    }
    else if ($('#action-type').val() == "SMS") {
        $('#allActions').append($('#allActions').innerHTML + "<tr><td> SMS </td><td>" + $('#action-sms').val() + "</td></tr>");
    }
}

我的模型:

public class AlertAction
{
    public List<string> Type { get; set; }

    public List<string> Contact { get; set; }
}

现在我当然只得到 1 个长字符串。我怎样才能解决这个问题?我想填写视图中的 2 个列表。但是 @Model.Actions.Type.Add() 不起作用。

4

1 回答 1

0

不久前,我从使用 stringify 切换到使用 postify,我个人发现使用它更好,因为它与 MVC 的默认模型绑定一起使用。(自述文件

示例用法:

var data = {Type: [], Contact: []};
for (var i = 1; i <= 5; i++) {
    data.Type.push("Type " + i.toString());
    data.Contact.push("Contact" + i.toString());
}
$.post('someurl', $.postify(data), function(response) {
    // process response
});
于 2013-11-11T15:55:17.540 回答