0

I am using ASP.NET MVC 3.

I have a view that accepts a view model of type EditGrantApplicationViewModel. In this view model I have properties. When the view is loaded for the first time I pass it an instance of this view model:

public ActionResult Create()
{
   EditGrantApplicationViewModel viewModel = new EditGrantApplicationViewModel();

   return View(viewModel);
}

On this view I have my submit button that takes the form values and adds it to the database. I also have another button on this view, but it doesn't have to be clicked, and when clicked, it takes the employee number, does a database lookup and gets the employee's details. This data is returned to te same view and prepopulates this view with the data so that the user doesn't have to type in the data. The data can either be retrieved this way or can be manually entered.

Then the user can go on and type in the other fields and edit any of the fields that were returned from the lookup. When doen then the user can click submit to add it to the database. How would I do something like this? Would I require to forms on my page, one going to the Create action method and the other going to GetEmployee action method to do the database lookup? Should I use multiple form on my page? If so is having multiple forms a best practice? Any code samples would be appreciated :)

4
  • What sort of data does the web service return? Commented Aug 3, 2011 at 13:14
  • Employee name, last name, address, branch, etc etc. Commented Aug 3, 2011 at 13:24
  • Sorry, I meant was it JSON, XML etc. Are you looking for examples of how you could do this via Ajax? Commented Aug 3, 2011 at 13:36
  • It might be a database call, still deciding. It needs to use the EditGrantApplicationViewModel. Commented Aug 4, 2011 at 7:49

2 Answers 2

2

You can use jquery to fire an AJAX call to return some JSON data when you click a button:

$("someButton").click(function() {
    $.ajax({
        url: "/Service/GetData",
        data: {}, // pass data here
        dataType: "json",
        type: "POST",
        success: function() {
            // manipulated return JSON data here
        }
    });
});

You could have a controller that calls the service and returns JSON or have the service do it and skip the controller. If you do it in a controller:

public ActionResult GetData() {
    var someData = service.GetData();

    return Json(someData);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Still deciding if it will be a web service call or do a database lookup. Yes I need to use the same EditGrantApplicationViewModel to populate these text fields. How would I do this if it is a normal database lookup and then return it to the view? I mean do I need 2 form tags on the page? Have you got some decent sample code for me please?
Could you please see if there is a similar example online as I don't understand your code. What do I specify in data: and success:?
Inside data: you place your data from your view to the controller. (ie. data: { EmployeeNum: $('#employeeNum').val()} (the name of your data should match your controller's parameter names) Success you place a function to complete on the return of your data, ie. fill in all of the empty fields success: function(data) { fillInFunction(data); } Where fillInFunction(data) takes a JSON object with the data to be entered, and fills in each of your fields ex.$('#EmployeeName').val(data.EmployeeName)
I still don't have an answer on this. I updated my original post, can you please see if it is a bit more clearer? I would understand better if I had some sample code from which to work.
0

Assuming the employee details are part of the EditGrantApplicationViewModel you should be able to just populate the fields on the form with the results of the web service call. Basically it should work just like a user manually entering the values. As long as your fields are named correctly the model binder will pick it up.

I'm making an assumption that your web service call is an asynch call from the page using javascript.

1 Comment

Still deciding if it will be a web serive or a database lookup. Yes I need to use the same EditGrantApplicationViewModel. Please see my reply to Dismissile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.