Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:56:27+00:00 2026-05-30T05:56:27+00:00

My situation is this: I have this ViewModel: public class RealtyViewModel { public RealtyViewModel()

  • 0

My situation is this:

I have this ViewModel:

public class RealtyViewModel
{
     public RealtyViewModel()
     {
          Realty = new Realty();
          Photos = new Collection<File>();
     }

     public Realty Realty { get; set; }

     public Collection<File> Photos { get; set; }
}

I pass this RealtyViewModel to my Edit.cshtml view. Inside the Edit view I call a Photos.cshtml partial view. The Photos partial view also uses the same @model RealtyViewModel.

Now, inside the Photos.cshtml partial view I do an AJAX request to delete a photo:

@Ajax.ImageActionLink
(@Url.Content(Model.Photos[i].Path), @Localization.Delete, "640", "480",
"DeletePhoto", new {realtyId = Model.Realty.Id, photoId = Model.Photos[i].Id},
new AjaxOptions()
                 {
                      Confirm = @Localization.DeleteConfirmation,
                      HttpMethod = HttpVerbs.Post.ToString(),
                      OnComplete = string.Format("deletePhotoFromPage('{0}')",
                                                                  Model.Photos[i].Id),
                      OnSuccess = "LoadCycle",
                      UpdateTargetId = "myDiv",
                      InsertionMode = InsertionMode.Replace

                 }, new {data_photoId = Model.Photos[i].Id})

I run this code:

[HttpDelete]
public ActionResult DeletePhoto(string realtyId, string photoId)
{
    Realty realty = DocumentSession.Load<Realty>(realtyId);

    realty.Photos.Remove(photoId);

    File photo = DocumentSession.Load<File>(photoId);

    // Deletes the file in the database
    DocumentSession.Advanced.DatabaseCommands.Delete(photoId, null);

    // Deletes the file in the disk
    System.IO.File.Delete(Server.MapPath(photo.Path));

    return new EmptyResult();
}

The problem is: my current realtyViewModel that I passed to the Edit view still references the photos I have deleted using the AJAX calls. Then when I try to save an updated model, it saves everything again holding the old references to the photos I have just deleted.

How can I update my model ( remove the deleted photos from [ model.Realty.Photos ] ) so that it reflects the current state of my Edit view?

Note: now it’s working because I’m using the Session object to store the Ids of deleted photos, but it’s not the way I think it should be. There must be a beautiful solution to this that just doesn’t come to my mind…

A beautiful solution would be: after a success deletion, the Ajax call should return the deleted photo Id so that I could remove it from [ model.Realty.Photos ]. Then, when I tried to save an edited Realty, it would reflect the changes correctly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:56:29+00:00Added an answer on May 30, 2026 at 5:56 am

    After a discussion on chat it seems that you have hidden fields in your DOM that you need to remove:

    @for (int i = 0; i < Model.Realty.Photos.Count(); i++)
    {
        @Html.HiddenFor(
            m => m.Realty.Photos[i], 
            new { data_photoid = Model.Realty.Photos[i] }
        )
    }
    

    Now you could have your controller action return the id of the photo that has to be deleted using JSON for example instead of an empty result:

    [HttpDelete]
    public ActionResult DeletePhoto(string realtyId, string photoId)
    {
        ... 
        return Json(new { photoId = photoId });
    }
    

    and on the client:

    function deletePhotoFromPage(result) { 
        ...
        $(':hidden[data-photoid="' + result.photoId + '"]').remove(); 
    }
    

    and also you should use the OnSuccess option instead of OnComplete in your Ajax link options:

    OnSuccess = "deletePhotoFromPage"
    

    instead of:

    OnComplete = string.Format("deletePhotoFromPage('{0}')", Model.Photos[i].Id),
    

    OnComplete could be invoked even in case of error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {
I have this situation: { float foo[10]; for (int i = 0; i <
I have this situation where I want to display a list of Administration objects
I have this situation: There are a login page with a login form (form
The situation is this: You have a Hibernate context with an object graph that
The situation is this: I have a shared (DLL) build of Qt 4.5.1 and
So, I have this situation where I need to see if an object is
In this situation I have two models, Comment and Score. The relationship is defined
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.