我有一些字段的视图,我想在这个视图中上传文件。当我使用表单提交方法上传它时,我在视图的文本框中丢失了输入的数据,我可以从 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();
}
});
}