0

There are some similar topics in forums, but there isn't any good answer. First question is that is it possible to pass httppostedfilebase to controller, when using modal popup? When I am using regular model and view, then everything is working well, but as soon I make this view as partialview, then everything except file is passing to controller. File is getting null value.

List View where modalpopup is excecuting:

<button style="cursor:pointer" type="submit" class="fluent-big-button" onclick="ShowModal('@Url.Action("Create", "Device")')" >

Modal Popup View:

@using (Html.BeginForm("Create", "Device", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="modal-body">
<div class="form-horizontal">           
    <fieldset>
        <legend>@Resources.Insert_New_Device</legend>   
        <div class="grid" style="margin: 0px auto">
            <div class="row" style="margin: 0px auto">
                <div class="span10">
                    <div class="row" style="margin: 0px auto"> 
                        <div style="white-space:nowrap" class="span2 readable-text"><label>@Resources.Device_No:</label> </div>
                        <div class="span3">               
                            <div class="input-control text" data-role="input-control">
                                @Html.TextBoxFor(m => Model.No, new { required = "required", autofocus = "autofocus", @tabindex="1"})
                                @Html.ValidationMessageFor(m => Model.No)
                                <button tabindex="-1" class="btn-clear" type="button"></button>
                            </div>
                        </div>
                        <div style="white-space:nowrap" class="span2 readable-text"><label>Tootja:</label> </div>
                        <div class="span3">               
                            <div class="input-control text" data-role="input-control">
                                @Html.TextBoxFor(m => Model.Manufacturer_Name, new { autofocus = "autofocus", @tabindex="2"})
                                @Html.ValidationMessageFor(m => Model.Manufacturer_Name)
                                <button tabindex="-1" class="btn-clear" type="button"></button>
                            </div>
                        </div>
                    </div>       

                    <div class="row" style="margin: 0px auto">

                        <div style="white-space:nowrap" class="span2 readable-text"><label>Pilt:</label> </div>
                        <div class="span3">                
                                <div class="fallback">
                                    <input type="file" name="DeviceImage" id="DeviceImage" />
                                </div>
                        </div>
                    </div>
                </div>
                <div class="row" style="margin: 0px auto">
                    <div class="span10">@Html.ValidationMessage("CustomError")</div>
                </div>
            </div>
        </div> 
    </fieldset>        
</div>       
</div>

<div class="modal-footer">
<div class="grid" style="margin: 0px auto">
            <div class="row" style="margin: 0px auto">
                <div class="span4"></div>
                <div class="span3"> 
                    <button class="button large span3" data-dismiss="modal">Cancel</button>
                </div>
                <div class="span3">
                    <input class="large span3 success" type="submit" value="Ok" />
                </div>
            </div>
</div>
</div>
} 

Controller:

    public ActionResult Create()
    {
        ViewBag.exsistingDevices = GetExsistingDevices();
        return PartialView("_Create");
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "Admin, Super")]
    public async Task<ActionResult> Create(Device device, HttpPostedFileBase DeviceImage)

    {
        try
        {
            if (ModelState.IsValid)
            {
                SPDBContext db = new SPDBContext();
                device.Last_Action_Date = DateTime.Now;


                // Insert image

                if (DeviceImage != null)
                {
                    if (DeviceImage.ContentLength > (2 * 1024 * 1024) && DeviceImage.ContentLength < 1)
                    {
                        ModelState.AddModelError("CustomError", "File size must be less than 2 MB");                          
                    }
                    var contentType = DeviceImage.ContentType;
                    device.Image = new byte[DeviceImage.ContentLength];
                    DeviceImage.InputStream.Read(device.Image, 0, DeviceImage.ContentLength);
                }
                db.Devices.Add(device);
                await db.SaveChangesAsync();
                return Json(new { success = true });
            }
        }
        catch (Exception e)
        {

        ModelState.AddModelError("CustomError", String.Format("{0}: {1}",Resources.Resources.Insert_Failed, e.Message));
        }
        return PartialView("_Create", device);
    }  

Model:

[NotMapped]
    public HttpPostedFileBase DeviceImage { get; set; }

public byte[] Image { get; set; }
  • Can you please edit your question and remove all the code that's not relevant to this problem? Cheers. – Inspector Squirrel Feb 25 '15 at 23:25
  • Please construct a MWE. I have a feeling what *might* be the problem, however, if that feeling is correct, then the real problem is not displayed here. Just edit the question as suggested before. – Florian Rappl Mar 09 '15 at 10:15
  • Thank You very much the probleem was with modal popup and serialize. Here is some more information and correct answer to that problem. http://stackoverflow.com/questions/26563289/file-upload-in-mvc-when-used-in-bootstrap-modal-returns-null – Raul Kasner Mar 09 '15 at 19:44

0 Answers0