-1

Why after uploading a file, i used debugger and get always file's value null. type = file is used only once. binding is working. i used debugger and can see the value of file before and after the binding, always null

Here is my view

@model WebApplication1.Models.Post

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add New Post</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostTitle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostTitle, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PostAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostAuthor, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostAuthor, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.WebSite, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.WebSite, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.WebSite, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PostDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.PostDate, new { @Value = @DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), @readonly = "readonly" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PostText, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostText, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostText, "", new { @class = "text-danger" })
            </div>
        </div>


        @{
         ViewBag.Title = "File Upload in MVC3 By Using Razor";
        }
        @using (Html.BeginForm("Create", "BlogController", FormMethod.Post , new { enctype = "multipart/form-data"}))
        {
            <div>
                <b><u>File Upload in MVC3 By Using Razor</u></b>
                Select Image
                <input type="file" name="file" />
                <input type="submit" value="Upload Image" name="Command" /><br />
            </div>
            <div>
                @ViewBag.Message
            </div>
        }



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to Posts List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Here is my create action in the BlogController

 public ActionResult Create([Bind(Include = "PostID,PostTitle,PostAuthor,WebSite,PostDate,PostText,PostImage,PostVideo")] Post post, HttpPostedFileBase file)
        {

            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
                    post.PostImage = file.FileName;
                    return RedirectToAction("Index");
                }
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");

        }
Tieson T.
  • 20,030
  • 4
  • 69
  • 86
Alex
  • 73
  • 1
  • 1
  • 8
  • Nothing seems amiss. What is the value of `Request["file"]`? Perhaps, it's just not binding to your action parameter for some reason. – Chris Pratt Sep 24 '15 at 20:14
  • @ChrisPratt. Where is Request["file"]. The value of file is null here: "if (file != null)" in the create action – Alex Sep 24 '15 at 20:17
  • The code you have shown works fine. But you have not shown the full view - do you have any other form controls with `name="file"`? –  Sep 24 '15 at 21:57
  • no, i added to my question the full view – Alex Sep 25 '15 at 09:49
  • 1
    Could it be the problem of nested forms? Can you place the form not inside another form and check the result? http://stackoverflow.com/questions/379610/can-you-nest-html-forms – Viktor Kireev Sep 25 '15 at 10:10
  • I got this message: Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /BlogController/Create Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.79.0 @ViktorKireev – Alex Sep 25 '15 at 10:51
  • Try replace "BlogController" by "Blog" – Viktor Kireev Sep 25 '15 at 10:53
  • It worked only with "blog" but if i make a new action "uplaodFIle". I need to uplaod the file wih the create (other fields to complete) how can i separate the form but use same method?@ViktorKireev – Alex Sep 25 '15 at 10:56
  • You have nested forms. Nested forms are invalid html and are not supported. –  Sep 25 '15 at 11:01
  • Now it is working , i don't have nester forms. But i have upload file action and create action with fields like name ,age and birthday. i want to click on the create button and make the profile with the two forms and upload the profile with picture, how can i do that? @StephenMuecke – Alex Sep 25 '15 at 11:07
  • 1
    It all needs to be inside one form (and with `new { enctype = "multipart/form-data" }`) –  Sep 25 '15 at 11:09

1 Answers1

0

Don't use nested forms, make a sepratlly action "uploadFile" and check file binding "HttpPostedFile Base file" same name like in the form

Alex
  • 73
  • 1
  • 1
  • 8