JonSick JonSick - 1 year ago 62
ASP.NET (C#) Question

Trying to load data into a new view

I have an issue attempting to create a "clone" of data on a page. The summary is that I have a create customer jobs screen and a separate manage customer jobs screen. These both work fine. I have a need to "clone" already existing customer job data to create a new similar set of jobs. So in my management screen, I have a clone button. I want this to head off to my "create" page with the data pre-populated with the original data, minus one or two pertinent identifying pieces of information, e.g. Job Name.

My issue is I cannot seem to get the post to redirect off to the Create page with my new data in the view model. I can't have a submit action as this saves the data; the data I'm cloning won't be persisted between the manage and create screens.

Here is where I'm at:


<button type="button" name="btnClone" value="btnClone" id="btnClone" formaction="CloneJob" class="btn btn-primary" style="width: 150px;">Clone</button>

This fires off a click event to grab the current jobId which then fires off this function:

function CloneJob(jobId) {
$.post('CloneJob', { JobId: jobId }, function (data) {


In my controller, I have this:

public ActionResult CloneJob(Guid jobId) {
// logic which wipes off the old data job from the view model
// eg names, ids, etc. but leaves the actual job details intact

return View("CreateJob", manageJobViewModel);

From this, I can see the HTML being returned in the IE/Chrome debug network section, but it won't go off to my CreateJob view populated with the data.

What am I missing?

Answer Source

I have managed to get this cracked.

My cshtml now contains this code:

@Html.ActionLink("Clone Job", "CloneJob", null, new { @class = "btn btn-default", @style = "width: 150px" })

My controller just grabs the current viewmodel from the session and uses it to clone the job. Then, the controller now simply returns the view with the new model I set up within the method.

return View("CreateJob",  clonedJobViewModel);

It's simple now when I look at it, but it's the getting there part.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download