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 8051605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:20:18+00:00 2026-06-05T07:20:18+00:00

I am updating an entity normally inside a controller as follows :- [HttpPost] public

  • 0

I am updating an entity normally inside a controller as follows :-

[HttpPost]
public ActionResult Create(Project project)
{
  //var model = new CompositeImageModel(0);
  if (ModelState.IsValid)
  {
    //var model = new CompositeImageModel(project.ProjectID);
    db.Projects.Add(project);
    db.SaveChanges();
    ViewBag.ProjectID = project.ProjectID;

    return RedirectToAction("Index");
  }

  return View();
}

And I wish to grab this new id “ViewBag.ProjectID” inside my Jquery AJAX code :-

onSaveBegin: function () {
  //CODE FOR SAVE BEGIN    
  $('#imageList').fadeIn('slow');
  $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/222"); }); 

},

instead of the hardcoded “222”.

How can I get it?

Thanks for your help and time

**********UPDATE****************************
So I have updated my Jquery as follows :-

<script type="text/javascript">
$(document).ready(function () {

    var imageList = $('#imageList'),
        submitButt = $('#submitButt');

    //hide the imageList initially
    imageList.hide(); //hide all but the first paragraph

    });


    var Project = {
        save: function () {
            var projectForm = $("#projectForm");
            if (projectForm.valid()) {
                projectForm.submit();
            }
            else {
                Project.onInvalidForm();
            }
        },
        onInvalidForm: function () {
            //CODE FOR INVALID FORM 
        },
        onSaveBegin: function () {
            //CODE FOR SAVE BEGIN    
            $('#imageList').fadeIn('slow');
        },
        onSaveSuccess: function () {
            //CODE FOR SAVE SUCCESS    
        },
        onSaveFailure: function () {
            //CODE FOR SAVE FAILURE    
            alert(ViewBag.ProjectID);
        },
        onSaveComplete: function () {
            //CODE FOR SAVE COMPLETE    
            //var hfProjectId = $("#hfProjectId").val();
            //$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
            $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); }); 
        }
    }; 

I have moved the code to onSaveComplete, since I should have the ProjectID then, but still am not able to get it yet.
however both options will not work.

**********UPDATE 2 ***************************

@model MvcCommons.Models.Project

@{
ViewBag.Title = "Create";

}

<h2>Create</h2>

@{    
Layout = "~/Views/Shared/_Layout.cshtml";    
Html.EnableClientValidation();    
Html.EnableUnobtrusiveJavaScript(); 

}

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript">
        $(document).ready(function () {

    var imageList = $('#imageList'),
        submitButt = $('#submitButt');

        //hide the imageList initially
    imageList.hide(); //hide all but the first paragraph

    });


    var Project = {
        save: function () {
            var projectForm = $("#projectForm");
            if (projectForm.valid()) {
                projectForm.submit();
            }
            else {
                Project.onInvalidForm();
            }
        },
        onInvalidForm: function () {
            //CODE FOR INVALID FORM 
        },
        onSaveBegin: function () {
            //CODE FOR SAVE BEGIN    
            $('#imageList').fadeIn('slow');
        },
        onSaveSuccess: function () {
            //CODE FOR SAVE SUCCESS    
        },
        onSaveFailure: function () {
            //CODE FOR SAVE FAILURE    
            alert(ViewBag.ProjectID);
        },
        onSaveComplete: function () {
            //CODE FOR SAVE COMPLETE    
            //var hfProjectId = $("#hfProjectId").val();
            //$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });

            $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); }); 
        }
    }; 

@{
var projectID = int.Parse(ViewBag.ProjectID);
}

@using (Ajax.BeginForm(
“/Create”,
new { },
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = “projectFormContainer”,
OnComplete = “Project.onSaveComplete”,
OnFailure = “Project.onSaveFailure”,
OnSuccess = “Project.onSaveSuccess”,
OnBegin = “Project.onSaveBegin”
},
new { id = “projectForm”, name = “projectForm” }))
{

         @Html.Hidden("hfProjectId", ViewBag.ProjectID)

        <div class="editor-label">          
            @Html.LabelFor(m => m.ProjectTitle)       
         </div>       
         <div class="editor-field">          
            @Html.TextBoxFor(m => m.ProjectTitle)          
            @Html.ValidationMessageFor(m => m.ProjectTitle)       
         </div>       
         <div class="editor-label">          
            @Html.LabelFor(m => m.ProjectText)       
         </div>       
         <div class="editor-field">          
            @Html.TextBoxFor(m => m.ProjectText)          
            @Html.ValidationMessageFor(m => m.ProjectText)       
         </div>       
         <p>       
            <input type="submit" value="Submit" onclick="Project.save();" />       
         </p>    
      </div>    
}


<div id='imageList'>
    <h2>Upload Image(s)</h2>
    @{ 
        Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel((int)ViewBag.ProjectID));
     }
</div>

@Html.ActionLink(“Back to List”, “Index”)

Code updated with latest changes

***********UPDATED*************************

Mode updates :-
Change the @Html.Hidden to this now :-

            @if(Model != null)
        {
             @Html.Hidden("hfProjectId", Model.ProjectID)
        }

The Javascript is still the same :=

            onSaveComplete: function () {
            //CODE FOR SAVE COMPLETE    
            var hfProjectId = $("#hfProjectId").val();
            alert(hfProjectId);
            $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
            //$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); }); 
        }

change the var projectID

@{
if (Model != null)  
{
    var projectID = Model.ProjectID;
}

}

in the controller, returning a “View”, with the model

        [HttpPost]
    public ActionResult Create(Project project)
    {
        if (ModelState.IsValid)
        {
            db.Projects.Add(project);
            db.SaveChanges();
            ViewBag.ProjectID = project.ProjectID;
            //return RedirectToAction("Index");
            return View("Create", project);
        }

        return View();
    }

Still no luck though!

********** FINAL UPDATE ****************************
I have decided to create a Project View Model and try to pass variables through it, since the ViewBag was not working in my case. So I changed the HttpPost Create function in the Controller to look as follows :-

        [HttpPost]
    public ActionResult Create(Project project)
    {
        ProjectModel viewModel = new ProjectModel();

        if (ModelState.IsValid)
        {
            db.Projects.Add(project);
            db.SaveChanges();
            viewModel.Project = project;
            return View("Index", viewModel);
        }

        return View();
    }

However this still did not refresh the ProjectID, and so I have decided to abandon this for the time being and try to come up with a better design. However if you could see soemthign wrong that I am doing please comment on this.

This is my final Create View :-

@model MvcCommons.ViewModels.ProjectModel
@{
var projectID = Model.Project.ProjectID;

}

@{
ViewBag.Title = “Create”;
}

Create

@{    
Layout = "~/Views/Shared/_Layout.cshtml";    
Html.EnableClientValidation();    
Html.EnableUnobtrusiveJavaScript(); 

}

`

`
$(document).ready(function () {

    var imageList = $('#imageList'),
        submitButt = $('#submitButt');

    //hide the imageList initially
    imageList.hide(); //hide all but the first paragraph

    });

    var Project = {
        save: function () {
            var projectForm = $("#projectForm");
            if (projectForm.valid()) {
                projectForm.submit();
            }
            else {
                Project.onInvalidForm();
            }
        },
        onInvalidForm: function () {
            //CODE FOR INVALID FORM 
        },
        onSaveBegin: function () {
            //CODE FOR SAVE BEGIN    
            alert('onSaveBegin ' + @(projectID));
            $('#imageList').fadeIn('slow');
        },
        onSaveSuccess: function () {
            //CODE FOR SAVE SUCCESS    
            //alert('onSaveSuccess')
        },
        onSaveFailure: function () {
            //CODE FOR SAVE FAILURE    
            //alert('onSaveFailure ' + @(projectID));
        },
        onSaveComplete: function () {
            //CODE FOR SAVE COMPLETE    
            //alert('onSaveComplete ' + @(projectID))
            //$("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(projectID)); });
            $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/" + hfProjectId); });
        }
    }; 

@using (Ajax.BeginForm(    
"/Create",    
new { },    
new AjaxOptions    
{       
    InsertionMode = InsertionMode.Replace,       
    UpdateTargetId = "projectFormContainer",       
    OnComplete = "Project.onSaveComplete",       
    OnFailure = "Project.onSaveFailure",
    OnSuccess = "Project.onSaveSuccess",
    OnBegin = "Project.onSaveBegin"    
},    
new { id = "projectForm", name = "projectForm" }))    
{    
    <div>       

        @*@Html.Hidden("hfProjectId", projectID)*@ 
        @Html.Hidden("hfProjectId", Model.Project.ProjectID) 

        <div class="editor-label">          
            @Html.LabelFor(m => m.Project.ProjectTitle)       
         </div>       
         <div class="editor-field">          
            @Html.TextBoxFor(m => m.Project.ProjectTitle)          
            @Html.ValidationMessageFor(m => m.Project.ProjectTitle)       
         </div>       
         <div class="editor-label">          
            @Html.LabelFor(m => m.Project.ProjectText)       
         </div>       
         <div class="editor-field">          
            @Html.TextBoxFor(m => m.Project.ProjectText)          
            @Html.ValidationMessageFor(m => m.Project.ProjectText)       
         </div>       
         <p>       

@* *@

}

<div id='imageList'>
    <h2>Upload Image(s)</h2>
    @{ 
        //Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel((int)ViewBag.ProjectID));
        Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(projectID));
     }
</div>

@Html.ActionLink(“Back to List”, “Index”)

  • 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-06-05T07:20:19+00:00Added an answer on June 5, 2026 at 7:20 am

    Easy way (Not very sure about it)

    $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+@(ViewBag.ProjectID) ); }); 
    

    The Normal way

    1- Put a HiddenField inside your view

    2- Get the value from ViewBag.ProjectID and assign it to this hidden field

    3- Read it with jQuery and then use it wherever you want.

    an Example should eb added to this answer right away.

    Example:

    Update

    //  Get the projectID value
        var projectID = int.parse(ViewBag.ProjectID);
    
    // add a hiddenfield and set it's value to the projectID
    @Html.Hidden("hfProjectId", projectID)
    
    // inside your jQuery code, get the hiddenField value
    var hfProjectId = $("#hfProjectId").val();
    
    // use this value inside your code
    $("#imageList").click(function () { $("#imageList").load("/File/ImageUpload/"+hfProjectId ); }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are able to create new entities without any issues, but updating an existing
I'm updating my model which is built by entity framework. I removed a entity
I have got a onetomany relation in Hibernate: @Entity public class Project @OneToMany(cascade =
When I instantiate a new entity, it looks like this: var myEntity = new
I have a routine which is updating my business entity. The update involves about
When updating a DataTable with 1850-ish new rows to a FbDataAdapter I get a
I am utilizing Entity Framework 4.0 and WCF. I am new to using Entity
i am on part Updating the blog entity where i need to load the
I have an entity model that has audit information on every table (50+ tables)
I have a JPA domain entity that I'm updating from user input. Depending on

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.