C# 将文件和模型传递到一个操作asp.net中

C# 将文件和模型传递到一个操作asp.net中,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个表单和一个上传程序(我使用PLUploader),希望用户填写文本框并在PLUploader中选择图像,然后单击提交按钮, 我将image和textboxs的值传递给一个动作,我编写了这个代码,但我总是在textboxs的值中得到null,但在动作中得到image 我认为这个问题与使用表单和PLuploader调用一个动作有关 public ActionResult Insert(News news, HttpPostedFileBase file) { // I get nul

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

我认为这个问题与使用表单和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;
            });
public ActionResult插入(新闻、HttpPostedFileBase文件)
{
//我在new中获取null,但在HttpPostedFileBase中获取文件
int结果=0;
HttpPostedFileBase FileData=Request.Files[0];
字符串文件名=null;
fileName=Path.GetFileName(FileData.fileName);
if(ModelState.IsValid)
{
//做点什么
}
其他的
{
返回视图(新闻);
}
}
@使用(Html.BeginForm(“Insert”、“News”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@LabelFor(model=>model.newsttitle)
@Html.TextBoxFor(model=>model.newsttitle,新的{@class=“form control”,@name=“title”})
@Html.ValidationMessageFor(model=>model.newsttitle)
您的浏览器不支持Flash、Silverlight、Gears、BrowserPlus或HTML5

    @LabelFor(model=>model.NewsText,new{@class=“text right”}) @Html.ValidationMessageFor(model=>model.NewsText) @Html.TextAreaFor(model=>model.NewsText,新{@rows=“10”、@cols=“80”、@class=“text editor”、@name=“title”}) 提交 } var uploader=$(“#uploader”).pluploadQueue({ //一般设置 运行时:“html5、gears、flash、silverlight、browserplus、html4”, url:'@url.Action(“插入”、“新闻”)', 最大文件大小:“10mb”, 块大小:“1mb”, 唯一的名称:true, 多重选择:错误, 多个队列:false, //指定要浏览的文件 过滤器:[ {标题:“图像文件”,扩展名:“jpg,png”} ], //闪光设置 flash_swf_url:'/Scripts/Moxie.swf', //Silverlight设置 silverlight_xap_url:“/Scripts/Moxie.xap” }) $('form')。提交(函数(e){ var uploader=$(“#uploader”).pluploadQueue(); //队列中的文件首先上载它们 如果(uploader.files.length>0){ //上传所有文件后,提交表单 uploader.bind('StateChanged',函数(){ if(uploader.files.length==(uploader.total.upload+uploader.total.failed)){ $('form')[0]。提交(); } }); uploader.start(); }否则{ 警报('您必须将至少一个文件排入队列'); } 返回false; });

    我怎样才能解决这个问题?我想在此操作中获取新闻和文件。

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

    public class NewsViewModel {
    
        public News News { get; set; }
        public HttpPostedFileBase File { get; set; }
    
    }
    
    public ActionResult Insert(NewsViewModel model) {
    
        /* ... */
    
    }
    
    创建视图时,将ViewModel传递到视图中。确保为输入字段使用正确的名称以使其正确绑定:

     @Html.TextBoxFor(model => model.File, new { type = "file" })
    

    我假设您可能必须告诉脚本文件输入应该有什么名称。

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

    public class NewsViewModel {
    
        public News News { get; set; }
        public HttpPostedFileBase File { get; set; }
    
    }
    
    public ActionResult Insert(NewsViewModel model) {
    
        /* ... */
    
    }
    
    创建视图时,将ViewModel传递到视图中。确保为输入字段使用正确的名称以使其正确绑定:

     @Html.TextBoxFor(model => model.File, new { type = "file" })
    
    我假设您可能必须告诉脚本文件输入应该有什么名称