开发者

Multiple submit buttons on same view using different action methods

开发者 https://www.devze.com 2023-03-24 17:59 出处:网络
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 in

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 th开发者_运维知识库e 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 :)


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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消