简体   繁体   中英

C# MVC 3 Partial View not hitting controller method

I have a create user page - it currently list a table with all current users in system. I can click create new button - this does a jQuery ajax submit which calls my controller method and returns a partial view and loads the new modal jQuery dialog box which contain all the fields ie Forename, Surname, etc. So I have my table in the background and this dialog box on centre of screen in modal view so it takes precedence. If a username is clicked the same method is called excpet a uniquie user id is passed in so the dialog form is not loaded blank - it is loaded with the current details of the user from the DB and as a user id was there a Delete button is added to the User dialog box as well as Save and Cancel which are there on create new.

Now for the problem - I have enclosed my Partial view page as below - I have also added its own js to the partial view as I need to show/hide diff dropdown boxes based on some choices made by user.

@using (Html.BeginForm("UserAction", "Admin", FormMethod.Post, new { id = "userActionForm" }))
{
//Fields on the dialog box....
}

on the dialog box then - I have buttons ie

<input id="DoDeleteUser" type="button" class="dialog-button" value="Delete User" style="margin: 5px" />

and then in the JS file for my page I have the following:

         $('#DoDeleteUser').click(function (event) {
          //alert("Delete Button Pressed"); - In for debugging
        $('#userID').val($(event.target).attr("userId")); - get id value into hidden field on page
        $('action').val('Delete'); - put action string into hidden field on page
        $('#userActionForm).submit();

      });

    $('#userActionForm').submit(function () {

       var formData = $("#userActionForm").serializeArray();

        $.ajax({
                url: this.action,
                type: this.method,
                data: formData,
            success: function (result) {
                $('#dialogContainer').html(result);
            }

        });
       return false;
});

My dialog container is the same container which the first page loads which I want to update when I come back from the UserAction method with a simple message saying "User Updated" or "User Deleted" and an OK button which when clicked would refresh the whole page (so the main table would be updated)

Then on my controller I have the method like:

   public ActionResult UserAction(UserModel model)
    {

        if (ModelState.IsValid)
        {
              if(model.Action == "Delete")

               //Go and do delete
               return PartialView ("UserActionSuccess", model);

          //close if etc etc

However I set a breakpoint on my UserAction method in my controller but it is never getting hit when I hit the Delete User button or the Save User button which is were I am stuck.

You're sending your post with form data, but you're expecting a model (json object) on your UserAction. You should use parameters instead to match the form input.

public ActionResult UserAction(string id)
{
    ...
}

You should consider using message classes for CRUD methods. It should make the logic a bit cleaner.

public class CreateUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class DeleteUser
{
    public int UserId { get; set; }
}

Then you would have a controller action for each CRUD method, like so:

public ActionResult Create(CreateUser message)
{
}

public ActionResult Delete(DeleteUser message)
{
}

In jQuery, you would prepare the AJAX call as follows:

$.ajax({
    url: "/yourcontroller/create",
    type: "POST",
    data: formData,
    success: function() { console.log('success'); },
    error: function() { console.log('error'); }
});

$.ajax({
    url: "/yourcontroller/delete/" + $("#userId").val(),
    type: "DELETE",
    success: function() { console.log('success'); },
    error: function() { console.log('error'); }
});

Finally, once you wire those up, you can use Firebug to check the jQuery side of things, and then use breakpoints in VS administrator mode to make sure the calls are populating your message objects correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM