将文件和模型传递到一个动作asp.net

时间:2015-10-21 07:26:56

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

我有一个表单和一个上传器(我使用PLUploader)并希望用户填充文本框并在PLUploader中选择图像,当单击提交按钮时, 我将图像和文本框值传递给一个动作,我编写了这段代码,但是我总是在textboxs值中得到null但是得到了图像。

我认为这个问题与使用form和PLuploader调用一个动作有关。

public ActionResult Insert(News news, HttpPostedFileBase file)
{
    // I get null in new but get file in HttpPostedFileBase
    int result = 0;

    HttpPostedFileBase FileData = Request.Files[0];

    string fileName = null;

    fileName = Path.GetFileName(FileData.FileName);

    if (ModelState.IsValid)
    {
       //do some thing
    }
    else
    {
        return View(news);
    }
}

@using (Html.BeginForm("Insert", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="col-xs-12">
        @Html.LabelFor(model => model.NewsTitle)
        @Html.TextBoxFor(model => model.NewsTitle, new { @class = "form-control",@name="title" })
        @Html.ValidationMessageFor(model => model.NewsTitle)
    </div>

    <div class="col-xs-12">
        <div id="uploader" class="img-plc">
            <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
        </div>
        <ul id="gallery"></ul>
    </div>
    <div class="col-xs-12">
        @Html.LabelFor(model => model.NewsText, new { @class = "text-right" })
        @Html.ValidationMessageFor(model => model.NewsText)

        @Html.TextAreaFor(model => model.NewsText, new { @rows = "10", @cols = "80", @class = "text-editor", @name = "title" })
    </div>

    <button type="submit">Submit</button>
}

var uploader = $("#uploader").pluploadQueue({
                // General settings
                runtimes: 'html5,gears,flash,silverlight,browserplus,html4',
                url: '@Url.Action("Insert", "News")',
                max_file_size: '10mb',
                chunk_size: '1mb',
                unique_names: true,
                multi_selection: false,
                multiple_queues: false,

                // Specify what files to browse for
                filters: [
                    { title: "Image files", extensions: "jpg,png" }
                ],

                // Flash settings
                flash_swf_url: '/Scripts/Moxie.swf',

                // Silverlight settings
                silverlight_xap_url: '/Scripts/Moxie.xap'

            })

    $('form').submit(function (e) {
                var uploader = $('#uploader').pluploadQueue();

                // Files in queue upload them first
                if (uploader.files.length > 0) {
                    // When all files are uploaded submit form
                    uploader.bind('StateChanged', function () {
                        if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                            $('form')[0].submit();
                        }
                    });

                    uploader.start();
                } else {
                    alert('You must queue at least one file.');
                }

                return false;
            });

我该如何解决这个问题?我想获得新闻并提交此行动。

1 个答案:

答案 0 :(得分:0)

创建一个ViewModel以包含两个属性

PFInstallation

创建视图时,将ViewModel传递到视图中。确保使用输入字段的正确名称使其正确绑定:

PFInstallation

我认为你可能不得不告诉你的脚本文件输入的名称。