Why everytime i need to empty the div if i want to add ajax resonse to my view. without this ajax respose not set to view here is my code.
$.ajax({
type: "POST",
url: "/JS/UploadImage/",
mimeType: "multipart/form-data",
processData: false,
contentType: false,
data: formdata,
success: function (result) {
if (result !== null) {
let parsed = JSON.parse(result);
if (parsed.data!== null) {
let substr = parsed.data.substring(14);
$("#rname").empty();
$("#rname").append(
'
' + substr + '
' )
}
else {
alert("Error occured while uploading");
}
}
},
error: function (result) {
alert("uploading failed");
}
});
is there any alternate way to do this. i want exactly what my code is doing but with the different approach. its my dottnet mvc app
Daniel WrightPosted Feb 18, 2025, 8:09 PM
It seems like your current approach involves emptying the target div (#rname) before appending the received data in order to ensure a fresh display. If you're looking for an alternative method to achieve the same result without explicitly emptying the div each time, you can consider utilizing jQuery's `.html()` method instead of `empty()` and `append()`.
Here's how you can modify your existing code snippet:
By using `.html()` to set the content of the div, you overwrite the existing content without needing to explicitly empty it first. This can streamline your code while achieving the same outcome. Remember to adapt this approach to suit the specific requirements and structure of your .NET MVC application. Let me know if you need further clarification or assistance!
umair mohsinPosted Feb 18, 2025, 8:16 PM
well thanks dear for your quick response but why i could not set values under success like this
$("#rname").val(substr) this is just a p tag on my page.