2012-03-14 51 views
0

我想在我的MVC视图中使用fileUpload,这是一个.aspx页面。 我想获取我的控制器中的对象的路径。在asp.net中的fileUpload MVC3.0

<% using (Html.BeginForm("AddClient","Client",FormMethod.Post,new {enctype ="multipart/form-data" })) 
    { %> 
<div class="float-left"> 
<%: Html.Label("BAAFile Path") %> 
<br /> 
<input type="file" name="file" id="file" /> 
<%: Html.ValidationMessageFor(model => model.BAAFilePath) %>--%> 
</div> 
<% } %> 

在页面的底部有一个按钮来保存细节(在fileupload控件旁边没有按钮)。

现在我想获取fileupload控制路径到我的控制器。

[HttpPost] 
    [ButtonName(Name = "Save")] 
    public ActionResult AddClient(ClientService.ClientDto client,HttpPostedFileBase file) 
    { 
     if (file != null && file.ContentLength > 0) 
      { 
       // extract only the fielname 
       var fileName = Path.GetFileName(file.FileName); 

      } 
    } 

现在我想要的文件路径

 var fileName = Path.GetFileName(file.FileName); 

提取到 “ClientService.ClientDto”,这是 “客户” 的对象。

 client.BAAFilePath=fileName 

但iam将空数据写入“var fileName”。

亚姆在 “的ActionResult了addClient(ClientService.ClientDto客户端)”

if (file != null && file.ContentLength > 0) 
    { 
     // extract only the fielname 
     var fileName = Path.GetFileName(file.FileName); 

    } 

我失去了任何使用下面的代码?

回答

1

我会建议将您的窗体绑定到视图模型,然后将您的视图模型绑定到DTO。 您的视图模型应该有一个类型为HttppostedFileBase的“文件”属性,然后在发布表单时您将正确地绑定所有内容。 请确保您的表单具有enctype =“multipart/form-data”

+0

我已编辑我的代码。添加ectype和HttppostedFileBase.still没有change.Iam只有null – Santosh 2012-03-14 08:52:24

+0

请看看我编辑的代码 – Santosh 2012-03-14 08:52:44

+0

当我使用masterpage,它不工作,如果一个采取.aspx没有masterpage其工作fine.found,因为它的“ asp:ScriptManager ID =“ScriptManager1”runat =“server”> “in master page.any help? – Santosh 2012-03-14 11:15:50