Pergunta

In my Create View I have

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

        <div>
            <h2>New Task</h2>
            <ol style="list-style-type: none;">
                <li>
                    @Html.LabelFor(m => m.Title, htmlAttributes: new { @class = "formlabel" })
                    @Html.TextBoxFor(m => m.Title)
                    @Html.ValidationMessageFor(m => m.Title)
                </li>
                <li>
                    @Html.LabelFor(m => m.Description, htmlAttributes: new { @class = "formlabel" })
                    @Html.TextAreaFor(m => m.Description)
                    @Html.ValidationMessageFor(m => m.Description)
                </li>
                <li>
                    @Html.LabelFor(m => m.Deadline, htmlAttributes: new { @class = "formlabel" })
                    @Html.TextBoxFor(m => m.Deadline, htmlAttributes: new { id = "date-picker", type = "text", @class = "hasDatepicker" })
                    @Html.ValidationMessageFor(m => m.Deadline)
                </li>
                <li>
                    @Html.LabelFor(m => m.RankID, htmlAttributes: new { @class = "formlabel" })
                    @Html.DropDownList("RankID", null, htmlAttributes: new { @class = "standselect" })
                    @Html.ValidationMessageFor(m => m.RankID)
                </li>
                <li>
                    @Html.LabelFor(m => m.PriorityID, htmlAttributes: new { @class = "formlabel" })
                    @Html.DropDownList("PriorityID", null, htmlAttributes: new { @class = "standselect" })
                    @Html.ValidationMessageFor(m => m.PriorityID)
                </li>
                <li>
                    <label for="uploadFile">Files</label>
                    <input type="file" name="uploadFile" id="uploadFile" />
                </li>
                <li style="margin: 20px 0 0 32px;">
                    <input type="submit" class="ghButton btn btn-navy" value="Create" />
                </li>
            </ol>
        </div>
    }

In my Controller I have

        [HttpPost]
        public ActionResult Create(ETaskModel taskModel, HttpPostedFileBase uploadFile)
        {
            var tasksServ = new TasksService();

            //var files = Request.Files;//files
            var upFiles = uploadFile;//up files

            //returning recently created task
            DataAccess.Task createdTask;
            tasksServ.Create(taskModel.Title, taskModel.RankID, SessionHelper.User.ID, taskModel.Deadline, taskModel.Description, taskModel.PriorityID,
                null,   //---------documents
                null,   //implementator users
                out createdTask);


            var generalServ = new General();
            ViewBag.RankID = new SelectList(generalServ.GetRanks(), "RankID", "RankValue", taskModel.RankID);
            ViewBag.PriorityID = new SelectList(generalServ.GetPriorities(), "PriorityID", "Name", taskModel.PriorityID);
            return View(taskModel);
        }

On submit I receive data in my ETaskModel taskModel object. but HttpPostedFileBase files is always empty. Also Request.Files.Count is always 0;

What's my problem. It possible to upload file(s) and receive ETaskModel data simultaneously?

P.S. uploadFile name of the file upload and controller method parameter are the same!

Foi útil?

Solução

I think you are using wrong overloaded version on BeginForm

Instead

Html.BeginForm(null, null, FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl, enctype = "multipart/form-data" })

Outras dicas

That is because your parameter in your Action needs to be named uploadFile instead of files to match the id provided on the form. Then, the file that was selected will be available.

Try adding the HttpPostedFileBase to your model,

    public class ETaskModel 
    {
      public string Title {get; set;}
      public string Description{get; set;}
      .
      .
      public HttpPostedFileBase uploadFile {get; set;}
    }

and in your controller,

        [HttpPost]
        public ActionResult Create(ETaskModel taskModel)
        {
          .
          .
        }

Didnt check the code, but this might work, Hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top