Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c# - Remove Image option in Edit Form

I want to delete image in edit form and to show the upload file option .How can I achieve this using Ajax since I'm using tab panes for multiple forms.

Here is my code,

Biodata.cshtml

<div class="form-group">
                <label class="control-label-staff"><b>Photo</b></label>
                @if (Model.ImageFiles != null)
                {
                    foreach (var item in Model.ImageFiles)
                    {
                        if (item.Name.Substring(0, 2) == "IM")
                        {
                            <span class="control-label-staff">
                                <img src="~/Documents/EmployeeAttachments/@Request.Query["EmpID"].ToString()/@item.Name" width="70px" height="70px" />
                            </span><br/>
                            <a asp-page-handler="RemoveImage" data-ajax="true" data-ajax-method="post" data-ajax-complete="RemoveImageCompleted">Delete</a>                            
                            
                        }
                    }
                }
                @if (Model.ImageFiles == null)
                {
                    <input type="file" asp-for="StaffPhoto" class="form-control-staff" accept="image/*" style="border:1px solid white;" />
                }
            </div>

Its not calling the asp-page-handler method. Directly executing the ajax method.

function RemoveImageCompleted(event) {
            if (event.responseText != "") {
                $("#Biodata").html(event.responseText);
            } else {
                alert("Image Has Been Deleted Successfully");
                $.ajax({
                    url:rootPath + "/Staff/Onboarding/Biodata",
                    type: "get",
                    success: function (result) {
                        $("#Biodata").html(result);
                        $('a[href="#Biodata"]').tab('show');
                    }
                })
            }
        }

This is my asp-page-handler method in BioData.cshtml.cs

public async Task<IActionResult> OnPostRemoveImageAsync()
        {
            string filename = Request.Form["filename"];
            if (filename != null)
            {
                var Folder = StaffBioData.EmpID.ToString();
                string filedel = Path.Combine(_env.WebRootPath, "Documents/EmployeeAttachments", Folder, filename);
                FileInfo fi = new FileInfo(filedel);
                if (fi != null)
                {
                    System.IO.File.Delete(filedel);
                    fi.Delete();
                } 
            }
            return new OkResult();

        }

Any help would be appreciated.Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Links are GET requests. You cannot post via a link; that is what forms are for. You'd need something like:

@if (Model.ImageFiles != null)
{
    foreach (var item in Model.ImageFiles)
    {
        if (item.Name.Substring(0, 2) == "IM")
        {
            @*<a asp-page-handler="RemoveImage" data-ajax="true" data-ajax-method="post" data-ajax-complete="RemoveImageCompleted">Delete</a>*@
            <form method="post" asp-page-handler="RemoveImage" data-ajax="true" data-ajax-method="post" data-ajax-complete="RemoveImageCompleted">
                <input type="submit" value="delete" />
            </form>
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...