文件的上传和持续的控制值 [英] File upload and persist values of controls

查看:110
本文介绍了文件的上传和持续的控制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字段的视图,我想上传文件中的这一观点。
当我把它用表单提交方法我松视图的文本框输入的数据上传,我上传它的jquery Ajax调用,这样我可以一气呵成还更新字段,或者我怎么能后门柱保持控制数据。

示范

 公共类SView:ViewBase
{
    公共字符串的客户{搞定;组; }
    公共字符串ProjectNumb {搞定;组; }
    公共字符串描述{搞定;组; }
    ....

控制器

 公众的ActionResult指数()
    {
        返回视图(如ViewBase.Current.Model SView);
    }    公众的ActionResult上传()
    {
        的foreach(在Request.Files串inputTagName)
        {
            HttpPostedFileBase文件= Request.Files [inputTagName]
            如果(file.ContentLength大于0)
            {
                字符串文件路径= HttpContext.Server.MapPath(〜/图片/)+ Path.GetFileName(file.FileName);                file.SaveAs(文件路径);
            }
        }        返回RedirectToAction(「指数」);
    }

查看

 < TR类=evendiv>
                    &所述; TD>
                        <标签类=labelfont>客户与LT; /标签>
                    < / TD>
                    &所述; TD>
                        <输入ID =tbxCust类型=文本VALUE =@ Model.Quotation.Customer/>
                    < / TD>
                < / TR>
                &所述; TR类=odddiv>
                    &所述; TD>
                        <标签类=labelfont>说明与LT; /标签>
                    < / TD>
                    &所述; TD>
                        < textarea的ID =tbxDescCOLS =20行=5> @ Model.Quotation.Description< / textarea的>
                    < / TD>
                < / TR>
                &所述; TR类=odddiv>
                    &所述; TD>
                        <标签类=labelfont>附加文件(S)LT; /标签>
                    < / TD>
                    &所述; TD>
                        @using(Html.BeginForm(上传,着陆,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
                        {
                            <输入ID =attchfile1类型=文件/>
                            <输入ID =Button1的类型=提交值=上传/>
                        }
                    < / TD>
                < / TR><输入ID =btnReqHrs类型=按钮值=申请时间的onclick =JavaScript的:requesthours()/><脚本类型=文/ JavaScript的>
功能requesthours(){
    $阿贾克斯({
        输入:POST,
        网址:Requesthours,
        数据:值,
        数据类型:JSON
        错误:功能(XHR,ajaxOptions,thrownError){
            // ERRORMSG(thrownError);
            $(#progressdiv),隐藏()。
        },
        成功:函数(){
            //$('#innerdiv').html(res);
            $(#progressdiv),隐藏()。
        }
    });
}


解决方案

我改变了我的看法按钮所以,在这里我通过使用jquery ajax调用,控制器被调用之前存储我的表单数据。

 <输入ID =Button1的类型=提交值=上传的onclick =StoreFormData/>

I have a view with some fields, i wanted to upload file in this view. When i upload it using Form Submit method i loose entered data in textboxes of the view, can i upload it from jquery ajax call so that i can update fields also in one go, or how can i maintain the controls data after post.

Model

public class SView : ViewBase
{
    public string Customer { get; set; }
    public string ProjectNumb { get; set; }
    public string Description { get; set; }
    ....

Controller

    public ActionResult Index()
    {
        return View(ViewBase.Current.Model as SView);
    }

    public ActionResult Upload()
    {
        foreach (string inputTagName in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[inputTagName];
            if (file.ContentLength > 0)
            {
                string filePath = HttpContext.Server.MapPath("~/Images/") + Path.GetFileName(file.FileName);

                file.SaveAs(filePath);
            }
        }

        return RedirectToAction("Index");
    }

View

<tr class="evendiv">
                    <td>
                        <label class="labelfont">Customer</label>
                    </td>
                    <td>
                        <input id="tbxCust" type="text" value="@Model.Quotation.Customer" />
                    </td>
                </tr>
                <tr class="odddiv">
                    <td>
                        <label class="labelfont">Description</label>
                    </td>
                    <td>
                        <textarea id="tbxDesc" cols="20" rows="5" >@Model.Quotation.Description</textarea>
                    </td>
                </tr>
                <tr class="odddiv">
                    <td>
                        <label class="labelfont">Attach File(s)</label>
                    </td>
                    <td>
                        @using (Html.BeginForm("Upload", "Landing", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            <input id="attchfile1" type="file" />
                            <input id="Button1" type="submit" value="Upload" />
                        }
                    </td>
                </tr>   

<input id="btnReqHrs" type="button" value="Request Hours" onclick="javascript:requesthours()" />

<script type="text/javascript">
function requesthours() {
    $.ajax({
        type: 'POST',
        url: Requesthours,
        data: values,
        dataType: "json",
        error: function (xhr, ajaxOptions, thrownError) {
            //ErrorMsg(thrownError);
            $("#progressdiv").hide();
        },
        success: function () {
            //$('#innerdiv').html(res);
            $("#progressdiv").hide();
        }
    });
}

解决方案

I changed my view button as so, where i store my form data by using jquery ajax call, before the controller is called.

<input id="Button1" type="submit" value="Upload" onclick="StoreFormData" />

这篇关于文件的上传和持续的控制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆