iam working on fileUpload in mvc.
my code is as follows:
Views/Client/AddClient.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Auditz.UI.Web.Automation.ClientService.ClientDto>" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h1>
Add A Client</h1>
<% using (Html.BeginForm("AddClient","Client",FormMethod.Post,new {enctype = "multipart/form-data" }))
{ %>
<%: Html.ValidationSummary(true) %>
<div class="tabcontrol">
<asp:Panel ID="pnlClientDtls" runat="server">
<asp:TabContainer ID="TabContainer" runat="server" Width="100%" ActiveTabIndex="1">
<asp:TabPanel ID="tb1" runat="server">
<HeaderTemplate>
Client Details
</HeaderTemplate>
<ContentTemplate>
<div class="formelements">
.....................
..............
</div>
Controllers/FileUploadController.cs
namespace FileUploadTest.Controllers
{
public class FileUploadController : Controller
{
//
// GET: /FileUpload/
public ActionResult FileUpload()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
}
}
Evrything works as desired with this code.
But if i place and in of Shared/Site.Master,iam getting null value in “HttpPostedFileBase uploadFile”.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
i cannot remove from my code as i want to add few ajax controls.
Make sure that you don’t nest HTML forms. So in your Master page when you open the
<form>tag ensure that you close it before rendering the view: