I want to display Image in modal pop up after form submission. But currently, I am getting the path C:\fakepath\Flag.jpg, not the Image. And also When I edit my form, I am not getting the Image.
Can anyone please suggest me where I am going wrong. Thank you.
Controller
[HttpPost]
public JsonResult _Edit(ClsEmployee clsEmployee, HttpPostedFileBase FilePath)
{
try
{
if (Request.Files.Count > 0)
{
FilePath = Request.Files[0];
if (FilePath != null && FilePath.ContentLength > 0)
{
string fileName = Path.GetFileName(FilePath.FileName);
string path = "/Image/" + fileName;
clsEmployee.FilePath = path;
}
}
context.Entry(clsEmployee).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
return Json(new { success = true });
}
catch
{
return Json(new { success = false });
}
}
Custom Image Helper
public static IHtmlString Image(this HtmlHelper helper, string src, string alt, string height, string width, params string[] allClasses)
{
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));//throws error
tb.Attributes.Add("alt", alt);
if (!string.IsNullOrEmpty(height) || !string.IsNullOrEmpty(width))
{
StringBuilder value = new StringBuilder();
if (!string.IsNullOrEmpty(height))
value.AppendFormat("height:{0};", height);
if (!string.IsNullOrEmpty(width))
value.AppendFormat("width:{0};", width);
tb.Attributes.Add("style", value.ToString());
}
if (allClasses?.Any() ?? false)
tb.Attributes.Add("class", string.Join(" ", allClasses));
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
View
<tr>
<td class="Title">
@item.Title
</td>
<td class="Link">
@item.Link
</td>
<td class="filePath">
<a target="_blank" data-id="@item.FilePath" href=@item.FilePath >
@Html.Image(item.FilePath, "Image", "60px", "", "img-thumbnail")
</a>
</td>
<td>
<a class="mdlEdit" data-toggle="modal" data-target="#EditRecord">Edit</a>
</td>
</tr>
Partial View Modal
@using(Html.BeginForm("_Edit", "Home", FormMethod.Post, new {id = "Editform", enctype = "multipart/form-data" }))
{
<div class="modal fade" id="EditRecord" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@Html.HiddenFor(m => m.Id)
@Html.EditorFor(model => model.Title)
@Html.EditorFor(model => model.Link)
@Html.TextBoxFor(m => m.FilePath, new { type = "file"})
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
}
Script to show existing data into modal
var row;
$(document).on('click', '.mdlEdit', function () {
row = $(this).closest('tr');
var title = $.trim(row.find('.Title').text());
var link = $.trim(row.find('.Link').text());
var filepath = $(this).data("Id");
$('#Title').val(title);
$('#Link').val(link);
$('#FilePath').attr('src', filepath);
})
Script to Submit the form
$('#Editform').submit(function () {
var formdata = new FormData($('#Editform').get(0));
$.ajax({
url: '@Url.Action("_Edit", "Home")',
type: 'POST',
data: formdata,
processData: false,
contentType: false,
success: function(response) {
if (response.success) {
row.find('.Title').text($('#Title').val());
row.find('.Link').text($('#Link').val());
row.find('.filePath').text($('#FilePath').val());
$('#Editform').get(0).reset();
$('#EditRecord').modal('hide');
}
}
});
return false;
})