Submitting the form for the correct action using ASP.NET and MVC

I have a simple form that is used to update an object in my ASP.NET MVC application. I want the user to be able to submit the form, invoke the appropriate action on my controller and then notify the user that the update is complete.

Since I wanted to display the jQuery dialog after the user clicked the refresh button, I wanted to use jQuery to submit the form. However, my problem is this:

Although I can easily get JQuery to trigger an action on my controller, I can't get it to trigger an action that I already set on my route and that was decorated [Authorize]and[AcceptVerbs(HttpVerbs.Post)]

For example, if a user updates their entity at the following URL:

/SomeController/Edit/5

When jQuery submits a form, I want to submit it to my editing action, which is already decorated, and takes the identifier as a parameter.

The jQuery that I use to submit the form is as follows:

$("#formFoo").submit(function() {
                var frm = $("#formFoo");
                var action = frm.attr("action");
                var serializedForm = frm.serialize();
                $.post(action, serializedForm, function() {
                    alert('Call completed');
                });
                return false;
            });

If I define my form as follows:

<form id="formFoo" action="/SomeController/SomeOtherAction" method="post">

and my action is as follows:

public void SomeOtherAction() 
{
    var myEntity=new MyEntity();
    UpdateModel(myEntity);
}

Then my action is called and my object is populated correctly, but its identifier field is not filled, so I do not know which object I am updating.

If I change my form to:

<form id="formFoo" action="/SomeController/Edit" method="post">

and I have an Edit action defined as:

[Authorize,AcceptVerbs(HttpVerbs.Post)]
public void Edit(int id, FormCollection collection)

Then my action never calls jQuery.

I suppose I could somehow pull the ID parameter out of the query string and pass in the data that I send back to the form, but that seems very ugly. Or is there a way I can get the ID field during SomeOtherAction action? Or am I just about this wrong?

+3
2

:

$.post(action

"" . , , , . URI "", URI, . MVC , , , , , URI, .

, firebug, , JavaScript action.

- , Net Firebug, XHR URI, . , . URI , , URI .

, jQuery Ajax Form plug-in. , .

+3

.

FormCollection .

FormCollection:

  • public ActionResult ProductEditHandle(int? productID, FormCollection formCollection)
    {
        Product product = null;
    
    if (productID.HasValue)
        product = ProductEditUpdateModel(productID.Value, formCollection);
    
    return View("ProductEdit", product);
    }
    
  • <% using (Html.BeginForm("ProductEditHandle", "Product", new { productID = 5 })) { %>
    ......
    <% } %>
    

:

  • public ActionResult AddressTypeHandler(AddressTypeDto addressTypeDto)
    {
        var addressType = addressTypeDto.AddressTypeId > 0 ? _addressService.UpdateAddressType(addressTypeDto) : _addressService.CreateAddressType(addressTypeDto);
    
    return RedirectToAction("AddressTypeEdit", new { addressTypeId = addressType.AddressTypeId });
    }
    
  • (, ViewPage - AddressTypeDto)

    <% using (Html.BeginForm("AddressTypeHandler", "People")) { %>
    
    <%= Html.Hidden("AddressTypeDto.AddressTypeId", (ViewData.Model != null) ? ViewData.Model.AddressTypeId : 0) %>
    <%=Html.TextBox("AddressTypeDto.Name", ViewData.Model != null ? ViewData.Model.Name : string.Empty, new { size = 50 })%>
    <% } %>
    
+1

Source: https://habr.com/ru/post/1705450/


All Articles