0

Good day,

I am trying to upload an image from my view

@using (Html.BeginForm("CrearCurso", "ProfesorCurso", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <div class="form-group">
    <label>Upload Image</label>
    <div class="input-group">
        <span class="input-group-btn">
            <span class="btn btn-default btn-file">
                Browse… <input type="file" id="imgInp">
            </span>
        </span>
        <input type="text" class="form-control" readonly>
    </div>
    <img id='img-upload'/>
</div>
}

I have this controller in mvc

  [HttpPost]
    public ActionResult CrearCurso(CursoViewModel CursoViewModel, HttpPostedFileBase imgInp)
    {

        return View();
    }

However when I inspect the HttpPsotedFileBase, it is empty. What is wrong here? thanks

1 Answer 1

2

Forms posts back the name/value pairs of its successful form controls. Your file input has no name attribute. Change it to

<input type="file" name="imgInp">

However, its better to strongly bind to your model, so add a

public HttpPostedFileBase ImgInp { get; set; }

property to your view model and use

@Html.TextBoxFor(m => m.ImgInp, new { type = "file" })

which will also allow you to add validation attributes to your file input if required

Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much, for simplicity I did not add it to a viewmodel
Why would you think that is simpler?
simpler for this example maybe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.