如何通过mvc form参数发送字符串

时间:2014-04-29 11:47:58

标签: c# jquery asp.net-mvc asp.net-mvc-4

我有这个表格 -

@using (Html.BeginForm("SaveVideo", "Upload", FormMethod.Post, new { id = "form-upload", @Class = "form-horizontal", enctype = "multipart/form-data", onsubmit = "return tags()", genres = "return genres()" }))
{
}

在表单提交时,我需要发送以逗号分隔的字符串。

<script type="text/javascript">
    function genres() {
        var genres = $('#input-genreautocomplete').val();
        return genres;
    }

    function tags() {
        var tags = $('#input-tagautocomplete').val();
        return tags;
    </script>

现在作为一个示例类型就像 - 23,15,16,22,11和标签一样。它返回我用逗号分隔的字符串。

现在我想在方法SaveVideo中使用这些字符串。但我无法将这些字符串作为参数。如何在方法上发送这些字符串?

自动填充功能如下 -

<script type="text/javascript">

    $(function () {

        $('#input-tagautocomplete').tagsinput({
            itemValue: 'Id',
            itemText: 'TagName',
            typeahead: {
                source: function (term, process) {
                    items = [];
                    map = {};
                    idofitem = [];
                    var url = "@Url.Content("~/Upload/GetTagNames/")";
                    return $.getJSON(url, { term: term }, function (data) {
                        $.each(data, function (i, item) {
                            map[item] = item;
                            items.push(item.TagName);

                        });
                        return (items);
                    });
                },
                updater: function (item) {
                    var selected = map[item].Id;
                    $('#tag-value').val(selected);
                    return item;
                }
            }
        });
    });
</script>

更新程序不起作用,虽然它是bootstrap的typeahead的扩展名。

1 个答案:

答案 0 :(得分:2)

我觉得你有点困惑。不需要在BeginForm()帮助程序中指定操作参数。事实上,我认为这样做没有任何意义。首先,这些输入应该在您的表单中,如果它们还没有:

@using (Html.BeginForm("SaveVideo", "Upload", FormMethod.Post, new { id = "form-upload", @Class = "form-horizontal", enctype = "multipart/form-data"}))
{
    <input type="text" id="input-tagautocomplete" name="tags" />
    <input type="text" id="input-genreautocomplete" name="genres" />
}

您还可以使用HTML帮助程序创建这些内容。重要的是,它们具有为name属性指定的值。

然后,您只需在操作方法中添加参数即可匹配这些名称:

public ActionResult SaveVideo(string tags, string genres)
{
     // do whatever you want with tags and genres
}