0

我有一些字段的视图,我想在这个视图中上传文件。当我使用表单提交方法上传它时,我在视图的文本框中丢失了输入的数据,我可以从 jquery ajax 调用上传它,以便我可以一次更新字段,或者我如何在发布后维护控件数据。

模型

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

控制器

    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");
    }

看法

<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();
        }
    });
}
4

2 回答 2

0

我想这可能是你的问题,试着改变你的模型,如下所示,

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

更改 Action 方法以获取发布值

[httppost]
public ActionResult Upload(SView sview)
    {
        HttpPostedFileBase myFile = Request.Files["MyFile"];
        if (myFile != null && myFile.ContentLength != 0)
        {
            string pathForSaving = Server.MapPath("~/Uploads");
            if (this.CreateFolderIfNeeded(pathForSaving))
              {
                try
                  {
                   myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName));
                  }
                  catch (Exception ex)
                  {
                    message = string.Format("File upload failed: {0}", ex.Message);
                   }
              }
         }
     }

如果目录不存在则创建目录的函数,

      private bool CreateFolderIfNeeded(string path)
        {
            bool result = true;
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception)
                { result = false; }
            }
            return result;
        }
于 2013-09-17T15:27:45.843 回答
0

我改变了我的视图按钮,在调用控制器之前,我使用 jquery ajax 调用来存储我的表单数据。

<input id="Button1" type="submit" value="Upload" onclick="StoreFormData" />
于 2013-09-19T15:51:29.090 回答